不强制执行线性布局权重和
Linear layout weight sum not enforced
我制作了一个 LinearLayout 来包含几行按钮。为了设置我可以在行中拥有的最大按钮数,我为每个按钮使用了 6 的权重和和 1 的布局权重,这样我可以在每行上有 6 个。但是,现在当我添加更多按钮时,它们会不断挤在同一行中。任何人都知道为什么它不看布局重量?
下面是我的布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
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" tools:context=".MainActivity"
android:weightSum="6"
android:clickable="false"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/eng1"
android:id="@+id/eng1"
android:background="@color/red"
android:textStyle="bold"
android:layout_weight="1"
android:layout_margin="10dp"
android:clickable="true"
android:onClick="displayMessage"
android:textSize="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cowl"
android:id="@+id/cowl"
android:textSize="20dp"
android:background="@color/red"
android:textStyle="bold"
android:clickable="true"
android:onClick="displayMessage"
android:layout_margin="10dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/DIFFNG"
android:id="@+id/diffng"
android:textSize="20dp"
android:background="@color/red"
android:textStyle="bold"
android:clickable="true"
android:onClick="displayMessage"
android:layout_margin="10dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/elec"
android:id="@+id/elec"
android:background="@color/red"
android:textStyle="bold"
android:clickable="true"
android:textSize="20dp"
android:onClick="displayMessage"
android:layout_margin="10dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/battt"
android:id="@+id/battt"
android:background="@color/red"
android:textStyle="bold"
android:clickable="true"
android:textSize="20dp"
android:onClick="displayMessage"
android:layout_margin="10dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/eng2"
android:id="@+id/eng2"
android:background="@color/red"
android:textStyle="bold"
android:clickable="true"
android:textSize="20dp"
android:onClick="displayMessage"
android:layout_margin="10dp"
android:layout_weight="1" />
</LinearLayout>
由于您已将 weight_sum 设置为 6,因此该布局中只能有 6 个 sub-childs。如果您添加超过 6 个,它们将被挤压。
使这个参数
android:layout_width="wrap_content"
在你所有的按钮中
android:layout_width="0dp"
如果你在水平布局中使用权重,你需要将孩子的宽度设置为 0dp。
你的 LinearLayout 的高度为 wrap_content .
我制作了一个 LinearLayout 来包含几行按钮。为了设置我可以在行中拥有的最大按钮数,我为每个按钮使用了 6 的权重和和 1 的布局权重,这样我可以在每行上有 6 个。但是,现在当我添加更多按钮时,它们会不断挤在同一行中。任何人都知道为什么它不看布局重量? 下面是我的布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
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" tools:context=".MainActivity"
android:weightSum="6"
android:clickable="false"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/eng1"
android:id="@+id/eng1"
android:background="@color/red"
android:textStyle="bold"
android:layout_weight="1"
android:layout_margin="10dp"
android:clickable="true"
android:onClick="displayMessage"
android:textSize="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cowl"
android:id="@+id/cowl"
android:textSize="20dp"
android:background="@color/red"
android:textStyle="bold"
android:clickable="true"
android:onClick="displayMessage"
android:layout_margin="10dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/DIFFNG"
android:id="@+id/diffng"
android:textSize="20dp"
android:background="@color/red"
android:textStyle="bold"
android:clickable="true"
android:onClick="displayMessage"
android:layout_margin="10dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/elec"
android:id="@+id/elec"
android:background="@color/red"
android:textStyle="bold"
android:clickable="true"
android:textSize="20dp"
android:onClick="displayMessage"
android:layout_margin="10dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/battt"
android:id="@+id/battt"
android:background="@color/red"
android:textStyle="bold"
android:clickable="true"
android:textSize="20dp"
android:onClick="displayMessage"
android:layout_margin="10dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/eng2"
android:id="@+id/eng2"
android:background="@color/red"
android:textStyle="bold"
android:clickable="true"
android:textSize="20dp"
android:onClick="displayMessage"
android:layout_margin="10dp"
android:layout_weight="1" />
</LinearLayout>
由于您已将 weight_sum 设置为 6,因此该布局中只能有 6 个 sub-childs。如果您添加超过 6 个,它们将被挤压。
使这个参数
android:layout_width="wrap_content"
在你所有的按钮中
android:layout_width="0dp"
如果你在水平布局中使用权重,你需要将孩子的宽度设置为 0dp。
你的 LinearLayout 的高度为 wrap_content .