我不明白如何使用这个属性。谁能多告诉我一点吗?


当前回答

请查看LinearLayout的weightSum和每个视图的layout_weight。android:weightSum="4" android:layout_weight="2" android:layout_weight="2"他们的layout_height都是0px,但我不确定这是相关的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4">

<fragment android:name="com.example.SettingFragment"
    android:id="@+id/settingFragment"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="2"
    />

<Button
    android:id="@+id/dummy_button"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="2"
    android:text="DUMMY"
    />
</LinearLayout>

其他回答

那样想的话,会简单些

如果你有3个按钮,它们的权重分别为1,3,1,它将像HTML中的表格一样工作

为这一行提供5份:1份用于按钮1,3份用于按钮2,1份用于按钮1

方面,

附加:

对于垂直方向,不要忘记设置高度为0dp

android:layout_height="0dp"

对于水平方向,不要忘记设置宽度为0dp

android:layout_width="0dp"

请查看LinearLayout的weightSum和每个视图的layout_weight。android:weightSum="4" android:layout_weight="2" android:layout_weight="2"他们的layout_height都是0px,但我不确定这是相关的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4">

<fragment android:name="com.example.SettingFragment"
    android:id="@+id/settingFragment"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="2"
    />

<Button
    android:id="@+id/dummy_button"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="2"
    android:text="DUMMY"
    />
</LinearLayout>

对我来说最好的解释之一是这个(来自Android教程,请参阅第7步):

layout_weight is used in LinearLayouts to assign "importance" to Views within the layout. All Views have a default layout_weight of zero, meaning they take up only as much room on the screen as they need to be displayed. Assigning a value higher than zero will split up the rest of the available space in the parent View, according to the value of each View's layout_weight and its ratio to the overall layout_weight specified in the current layout for this and other View elements. To give an example: let's say we have a text label and two text edit elements in a horizontal row. The label has no layout_weight specified, so it takes up the minimum space required to render. If the layout_weight of each of the two text edit elements is set to 1, the remaining width in the parent layout will be split equally between them (because we claim they are equally important). If the first one has a layout_weight of 1 and the second has a layout_weight of 2, then one third of the remaining space will be given to the first, and two thirds to the second (because we claim the second one is more important).

顾名思义,布局权重指定了特定字段或小部件应该占据屏幕空间的数量或百分比。 如果我们在水平方向上指定权重,那么我们必须指定layout_width = 0px。 类似地,如果我们在垂直方向上指定权重,那么我们必须指定layout_height = 0px。