I've created an xml layout in my android project, but it does not fit on all screens. Is there any way to fit it on all screens?

Here is the layout content:

<TableLayout android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <TableRow android:id="@+id/tableRow1" android:layout_width="100dp" android:layout_height="100dp">
        <ImageButton android:src="@drawable/unlock" android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/imageButton1"></ImageButton>
        <ImageButton android:src="@drawable/lockicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton3"></ImageButton>
        <ImageButton android:src="@drawable/lockicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton4"></ImageButton>
        <ImageButton android:src="@drawable/lockicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton6"></ImageButton>
        <ImageButton android:src="@drawable/lockicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton7"></ImageButton>
    </TableRow>
    <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <ImageButton android:layout_width="wrap_content" android:id="@+id/imageButton2" android:src="@drawable/lockicon" android:layout_height="wrap_content"></ImageButton>
        <ImageButton android:src="@drawable/lockicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton5"></ImageButton>
        <ImageButton android:src="@drawable/lockicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton8"></ImageButton>
        <ImageButton android:src="@drawable/lockicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton9"></ImageButton>
        <ImageButton android:src="@drawable/lockicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton10"></ImageButton>
    </TableRow>
    <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <ImageButton android:src="@drawable/lockicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton11"></ImageButton>
        <ImageButton android:src="@drawable/lockicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton12"></ImageButton>
        <ImageButton android:src="@drawable/lockicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton13"></ImageButton>
        <ImageButton android:src="@drawable/lockicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton14"></ImageButton>
        <ImageButton android:src="@drawable/lockicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton15"></ImageButton>
    </TableRow>
</TableLayout>
有帮助吗?

解决方案

Try setting the width of TableRow to match_parent for all rows. Also add weights to all the ImageButton components.

其他提示

In your TableLayout use android:layout_weightSum and then in the 3 children, specify the weights you desire.

The problem is, Your first child has a constant measurement of 100dp. So this will not work on all screen sizes because they're changing and your measurement specifications are not.

// try this way
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp">
        <ImageButton
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:id="@+id/imageButton1"/>
        <ImageButton
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:id="@+id/imageButton3"/>
        <ImageButton
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:id="@+id/imageButton4"/>
        <ImageButton
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:id="@+id/imageButton6"/>
        <ImageButton
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:id="@+id/imageButton7"/>
    </TableRow>
    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp">
        <ImageButton
            android:id="@+id/imageButton2"
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"/>
        <ImageButton
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:id="@+id/imageButton5"/>
        <ImageButton
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:id="@+id/imageButton8"/>
        <ImageButton
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:id="@+id/imageButton9"/>
        <ImageButton
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:id="@+id/imageButton10"/>
    </TableRow>
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp">
        <ImageButton
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:id="@+id/imageButton11"/>
        <ImageButton
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:id="@+id/imageButton12"/>
        <ImageButton
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:id="@+id/imageButton13"/>
        <ImageButton
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:id="@+id/imageButton14"/>
        <ImageButton
            android:src="@drawable/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:id="@+id/imageButton15"/>
    </TableRow>
</TableLayout>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top