嵌套线性布局中的布局权重

Layout weight in nested linear layout

所以我想在 tic tac toe 布局中有 6 个按钮,为此我使用嵌套线性布局。

这是布局的 xml 文件:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:gravity="center"
    tools:context=".TicTacToeMainActivity$PlaceholderFragment">



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title"
        android:id="@+id/title"
        android:textSize="20dp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="horizontal">



    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_weight="1"
        android:id="@+id/topright"
        android:onClick="right_click" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_weight="1"

        android:id="@+id/topcenter"
        android:onClick="right_click" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_weight="1"

        android:id="@+id/topleft"
        android:onClick="right_click" />

</LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:layout_weight="1"
            android:id="@+id/midright"
            android:onClick="right_click" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:layout_weight="1"

            android:id="@+id/midcenter"
            android:onClick="right_click" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:layout_weight="1"

            android:id="@+id/midleft"
            android:onClick="right_click" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:layout_weight="1"
            android:id="@+id/lowright"
            android:onClick="right_click" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:layout_weight="1"

            android:id="@+id/lowcenter"
            android:onClick="right_click" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:layout_weight="1"

            android:id="@+id/lowleft"
            android:onClick="right_click" />

    </LinearLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reset"
        android:id="@+id/reset"
        android:onClick="right_click" />
</LinearLayout>

但是,这会呈现以下屏幕:

如何让按钮调整大小,使每一行与其他行占据相同数量的 space,并且没有像此屏幕截图中那样的任何边距?也忽略按钮中引用的方法,因为它们不是制作的。

将 9 个 Buttonwrap_content 的所有实例替换为 match_parent。然后为了补偿标题和底部按钮占用的间距,将 3 horizontally-oriented LinearLayout 的高度更改为 0dp。他们的布局权重“1”应该会导致他们在标题和底部按钮布局后填充可用空间的三分之一 space。

在您的情况下,当您对按钮使用 wrap_content 时,按钮的大小将使其高度刚好足以包含其中的文本。使用 match_parent 将扩展每个按钮(以及每个内部 LinearLayout)以填充它可用的所有 space。

您需要进行两项更改,

1. 如果你给 weightLinearLayout 然后写 layout_height="0dp"

2.按钮的高度必须是match_parent

所以你的布局看起来像,

<LinearLayout>
    <TextView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"   //see the change here
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent" //change height to match_parent
            android:text=""
            android:layout_weight="1"
            android:id="@+id/topright"
            android:onClick="right_click" />

       //do same changes to rest two buttons within this LinearLayout

       .
       .
    </LinearLayout>

    <LinearLayout> //make same changes here

        <Button/>  //and here also
        <Button/>  //here also
        <Button/>  //here also
    </LinearrLayout>

    <LinearLayout> //make same changes here

        <Button/>  //and here also
        <Button/>  //here also
        <Button/>  //here also
    </LinearrLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="reset"
        android:id="@+id/reset"
        android:onClick="right_click" />
</LinearLayout>