如何使用表布局填充带有四个按钮的屏幕?

时间:2011-08-25 11:45:27

标签: android android-layout android-tablelayout

我有以下布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:id="@+id/relativeLayout1" android:layout_height="fill_parent">
    <TableLayout android:id="@+id/TableLayout01" android:stretchColumns="*"
        android:layout_height="wrap_content" android:layout_width="fill_parent"
        android:layout_centerInParent="true" >
        <TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <Button android:id="@+id/syncDownload" android:layout_margin="5dp" android:layout_weight="1"
                android:layout_height="fill_parent" android:layout_width="fill_parent"
                android:text="Download1" android:textColor="#EDFF99"
                android:textStyle="bold" android:background="@drawable/custom_button" />
            <Button android:id="@+id/branchesButton" android:layout_weight="1"
                android:layout_margin="5dp" android:layout_height="wrap_content"
                android:layout_width="fill_parent" android:text="Download2"
                android:textColor="#EDFF99" android:textStyle="bold"
                android:background="@drawable/custom_button" />
        </TableRow>
        <TableRow android:id="@+id/TableRow02" android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <Button android:id="@+id/attendanceButton" android:layout_weight="1"
                android:layout_margin="5dp" android:layout_height="wrap_content"
                android:layout_width="fill_parent" android:text="Upload1"
                android:textColor="#EDFF99" android:textStyle="bold"
                android:background="@drawable/custom_button" />
            <Button android:id="@+id/syncUpload" android:layout_margin="5dp" android:layout_weight="1"
                android:layout_height="wrap_content" android:layout_width="fill_parent"
                android:text="Upload2" android:textColor="#EDFF99" android:textStyle="bold"
                android:background="@drawable/custom_button" />
        </TableRow>
    </TableLayout>
</RelativeLayout>

哪个出局

enter image description here

如何拉伸四个按钮以填满整个屏幕?

2 个答案:

答案 0 :(得分:15)

您不需要TableLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">
        <Button
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            ... />
        <Button
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            ... />
    </LinearLayout>
    <LinearLayout
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">
        <Button
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            ... />
        <Button
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            ... />
    </LinearLayout>
</LinearLayout>

答案 1 :(得分:2)

使用TableLayout

fill_parent身高更改为android:layout_height="fill_parent"

使用TableRow

将每个android:layout_height="0px"的高度更改为0px

并使用android:layout_weight="1"

将每个人的体重设置为1

最后不要忘记将Button的身高设置为fill_parent

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TableLayout android:id="@+id/TableLayout01" android:stretchColumns="*"
        android:layout_height="fill_parent" android:layout_width="fill_parent"
        android:layout_centerInParent="true" >
        <TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent"
             android:layout_height="0px"  android:layout_weight="1" >
            <Button android:layout_margin="5dp" android:layout_weight="1"
                android:layout_height="fill_parent" android:layout_width="fill_parent"
                android:text="Download1" android:textColor="#EDFF99"
                android:textStyle="bold" />
            <Button android:layout_weight="1"
                android:layout_margin="5dp" android:layout_height="fill_parent"
                android:layout_width="fill_parent" android:text="Download2"
                android:textColor="#EDFF99" android:textStyle="bold" />
        </TableRow>
        <TableRow android:layout_width="fill_parent"
            android:layout_height="0px" android:layout_weight="1" >
            <Button android:layout_weight="1"
                android:layout_margin="5dp" android:layout_height="fill_parent"
                android:layout_width="fill_parent" android:text="Upload1"
                android:textColor="#EDFF99" android:textStyle="bold"/>
            <Button android:layout_margin="5dp" android:layout_weight="1"
                android:layout_height="fill_parent" android:layout_width="fill_parent"
                android:text="Upload2" android:textColor="#EDFF99" android:textStyle="bold"/>
        </TableRow>
    </TableLayout>
</RelativeLayout>