【发布时间】: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'