【问题标题】:android:Why "layout_weight=1" makes the view align parent bottom in linearlayout? [duplicate]android:为什么“layout_weight = 1”使视图在线性布局中对齐父底部? [复制]
【发布时间】:2016-11-12 22:12:27
【问题描述】:

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>

事实证明,textview 将被放置在其父级的底部。我认为 layout_weight 用于分配未使用的空间,并且将 layout_weight 与以下代码一起使用是很常见的:

    android:layout_width="0dp"

但在这种情况下,第一个布局要求占用所有空间:

    android:layout_width="match_parent"
    android:layout_height="match_parent

那么布局权重在这里是如何工作的呢?
PS:我读过这个问题:What does android:layout_weight mean? 但我不认为它解释了这个问题。

layout_weight 指定布局中要分配给 View 的额外空间的多少。

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

【问题讨论】:

  • 当你使用 layout_weight 时,在水平方向上设置 layout_widht = '0dp',在垂直方向上设置 layout_height='0dp'

标签: android android-layout


【解决方案1】:

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

由于您没有提到所有其他视图的权重,它的行为是错误的。

layout_weight 在您希望子视图占父视图的一定百分比时很有用。

例如,

在父视图中您需要提及:

android:weightSum="1"

因此,您的父视图的总权重为 1,并且在两个视图中您都需要提及:

android:layout_weight=".9"android:layout_weight=".1"

所以第一个视图将占用 90% 的空间,第二个视图将占用 10% 的空间。

为了更清楚,理想情况下,所有孩子的权重总和应该等于父母中提到的weightsum,这样才能按预期工作。

**正如你提供的android:layout_width,android:layout_height`的textview,这是错误的,因为它会在重量上产生问题。

所以要正确使用权重属性,您需要将其他规范指定为0dp,以便权重成功应用。**

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

要实际理解它,你为什么不玩弄下面 布局:

只需尝试更改 linear_layout、text_view 的权重,您就会看到它应该如何理想地工作:

<?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>

【讨论】:

  • 感谢回答。我了解layout_weight的常见用法。我一开始打算做的是将textview放在一个linearlayout的底部。我在问题中发布的布局可以实现这个目标。我只是想知道是否有人可以解释它是如何工作的。
  • 我试图解释错误,请在答案中阅读它,如果您仍有任何疑问,请告诉我,我很乐意为您提供帮助!干杯。
【解决方案2】:

如果您想将组件放置在布局中的单独框中,您应该使用LinearLayout。 您可以使用orientation垂直水平定义盒子放置方式。 您可以使用layout_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_linear 将您的布局垂直分成两部分(因为您使用 2 个组件)。现在您可以将权重设置为子组件的宽度。所以,(对于 TextView)你设置了 android:layout_width="0dp" 的宽度和 android:layout_weight="2" ..跟随它的 LinearLayout- 。 这样做的结果是 parent_layout 将自己分成 6 个部分(2+4=6),并为 LinearLayout 分配 4 个部分,为 TextView 分配 2 个部分。

【讨论】:

    猜你喜欢
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 2019-04-03
    相关资源
    最近更新 更多