如何让两个按钮连续大小相同?

How to give two buttons are the same size in a row?

我试着让两个按钮的大小相同。我使用以下代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <TableRow android:id="@+id/tableRow5" >
                <Button
                    android:id="@+id/btn_pre"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_span="1"
                    android:text="@string/pre" />
                <Button
                    android:id="@+id/btn_next"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_span="1"
                    android:text="@string/next" />
            </TableRow>
        </TableLayout>
    </LinearLayout>
</ScrollView>

我的错误在哪里?请指教

我认为你应该使用 LinearLayout 而不是 tableLayout。并添加

  android:layout_weight="1"

按钮的属性。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
                <Button
                    android:id="@+id/btn_pre"
                    android:layout_weight="1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_span="1"
                    android:text="@string/pre" />
                <Button
                    android:id="@+id/btn_next"
                    android:layout_weight="1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_span="1"
                    android:text="@string/next" />

        </LinearLayout>
    </LinearLayout>
</ScrollView>

您仍然可以使用 TableRow,因为它是 LinearLayout 的子 class:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <TableRow android:id="@+id/tableRow5" >
                <Button
                    android:id="@+id/btn_pre"
                    android:layout_width="0dp"
                    android:weight="1"
                    android:layout_height="wrap_content"
                    android:layout_span="1"
                    android:text="@string/pre" />
                <Button
                    android:id="@+id/btn_next"
                    android:layout_width="0dp"
                    android:weight="1"
                    android:layout_height="wrap_content"
                    android:layout_span="1"
                    android:text="@string/next" />
            </TableRow>
        </TableLayout>
    </LinearLayout>
</ScrollView>

如果您只想在一行中放置两个按钮,则不需要 ScrollViewTableLayoutTableRow

只是一个带有两个Button的水平LinearLayout,其中layout_weight属性设置为0.50,意思是占space的50%(要使 layout_weight 起作用,您必须将 layout_width 设置为 0dp)。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn_pre"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.50"
        android:text="@string/pre" />
    <Button
        android:id="@+id/btn_next"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.50"
        android:text="@string/next" />
</LinearLayout>

还有更好的方法是使用 match_parent 而不是 fill_parent,因为正如 Android 文档中所说:

FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)

see more in android documentation.