【Interface&navigation】线性布局(12)

LinearLayout是一个视图组,它可以垂直或水平地沿一个方向排列所有儿童。您可以使用android:orientation属性指定布局方向 。

注意: 为了获得更好的性能和工具支持,您应该使用ConstraintLayout构建布局。

【Interface&navigation】线性布局(12)
a的所有孩子LinearLayout都是一个接一个堆叠起来的,所以垂直列表每行只有一个孩子,不管它们有多宽,一个水平列表将只有一行高(最高的孩子的身高,加上填充)。A LinearLayout尊重儿童与每个儿童的重力(右,中心或左对齐)之间的差距。

布局权重


同等重视的儿童
要创建一个线性布局,其中每个孩子在屏幕上使用相同的空间量,请将android:layout_height每个视图设置为"0dp"(对于垂直布局)或将android:layout_width每个视图设置为"0dp"(对于水平布局)。然后将android:layout_weight每个视图设置为"1"。

LinearLayout还支持为具有该属性的个别孩子分配 权重android:layout_weight。该属性根据它在屏幕上占用多少空间来为视图分配“重要性”值。较大的重量值允许它扩大以填充父视图中的任何剩余空间。子视图可以指定一个权重值,然后视图组中的任何剩余空间按其声明权重的比例分配给子项。默认重量为零。

例如,如果有三个文本字段,其中两个声明权重为1,而另一个没有权重,则没有权重的第三个文本字段不会增长,只会占用其内容所需的区域。在测量完所有三个场之后,另外两个将平均扩展以填充剩余的空间。如果第三个字段的权重为2(而不是0),则现在声明比其他两个字段重要,因此它占总剩余空间的一半,而前两个字段平分。


<?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:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/to" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/subject" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="@string/message" />
    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="@string/send" />
</LinearLayout>

【Interface&navigation】线性布局(12)

有关a的每个子视图可用的属性的详细信息LinearLayout,请参阅LinearLayout.LayoutParams。

联系我

QQ:94297366
微信打赏:https://pan.baidu.com/s/1dSBXk3eFZu3mAMkw3xu9KQ

公众号推荐:

扫描二维码关注公众号,回复: 1778839 查看本文章

【Interface&navigation】线性布局(12)

猜你喜欢

转载自blog.51cto.com/4789781/2134038