Android 安卓:为什么;布局图“重量=1”;使视图在linearlayout中与父底部对齐?

Android 安卓:为什么;布局图“重量=1”;使视图在linearlayout中与父底部对齐?,android,android-layout,Android,Android Layout,xml布局如下所示: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Linear

xml布局如下所示:

    <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_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
        </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bottom"/>
    </LinearLayout>
但在这种情况下,第一个布局要求使用以下内容占用所有空间:

    android:layout_width="match_parent"
    android:layout_height="match_parent
那么布局权重是如何工作的呢?
附言:我读过这个问题:但我不认为它能解释这个问题

布局权重指定布局中要分配给视图的额外空间量

第一个linearlayout已经使用属性match_parent占据了整个空间,为什么设置layout_weight会使第二个视图显示在底部?
我相信这不是布局权重的常用用法。希望有人指出我的错误。

当您使用
布局权重属性时,它用于计算单亲视图的子视图的权重

因为你们并没有提到所有其他观点的权重,所以它的行为是错误的

当您希望子视图占父视图的一定百分比时,
layout\u weight
非常有用

比如说,

在父视图中,您需要提及:

android:weightSum="1"
因此,父视图的总权重为1,在这两个视图中,您都需要提到:

android:layout\u weight=“.9”
android:layout\u weight=“.1”

因此,第一个视图将占据90%,第二个视图将占据10%的空间

为了更清楚,理想情况下,所有孩子的权重之和应等于父母中提到的
weightsum
,以使其按预期工作

**正如您提供的textview的
android:layout\u width,
android:layout\u height`一样,这是一个错误,因为它会在重量上产生问题

因此,要正确使用权重属性,您需要将其他规格设置为
0dp
,以便成功应用权重**

注意:当您使用重量其他规格时,如
android:layout\u width,
android:layout_height
应设置为
0dp

为了实际理解它,你为什么不在下面玩玩呢 布局:

只要试着改变线性布局、文本视图的权重,你就会看到它的理想工作方式:


如果要将组件放置在布局中的单独框中,应使用
LinearLayout
。 您可以使用
方向
垂直或水平来定义框的放置方式。
您可以使用
layout\u weight
轻松定义它们的大小。 看这里:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id=@+id/parent_linear>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4">
    </LinearLayout>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="bottom"/>
</LinearLayout>

parent\u linear
将布局垂直分为两部分(因为您使用了两个组件)。现在可以将权重设置为子组件的宽度。因此,(对于TextView)您可以为其宽度设置
android:layout\u width=“0dp”
,并为其设置
android:layout\u weight=“2”
。按照它进行线性布局-。
其结果是父布局将自身划分为6个部分(2+4=6),并将4个部分分配给LinearLayout,将2个部分分配给TextView

当你使用layout\u weight使layout\u widt在水平方向上为'0dp',并使layout\u height在垂直方向上为'0dp'时,答案是anks。我理解layout\u weight的常用用法。我打算在开始时将文本视图放在线性布局的底部。我在问题中发布的布局可以实现这一点目标。我只是想知道是否有人可以解释它是如何工作的。我试图解释错误,请在回答中阅读它,并让我知道如果你还有任何疑问,我将很乐意帮助你!!干杯
<?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"
    android:weightSum="100" >

    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:background="@android:color/holo_blue_bright" >
    </LinearLayout>

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="90"
        android:background="@android:color/white"
        android:text="bottom"
        android:textColor="@android:color/black" />

</LinearLayout>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id=@+id/parent_linear>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4">
    </LinearLayout>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="bottom"/>
</LinearLayout>