Linux 拨号vps windows公众号手机端

linearlayout中怎么指定子元素所占权重

lewis 8年前 (2017-01-14) 阅读数 7 #程序编程
文章标签 linearlayout

在LinearLayout中,可以使用android:layout_weight属性来指定子元素所占的权重。该属性值是一个浮点数,用于指定子元素在父布局中所占的比例。

例如,如果一个LinearLayout中有两个子元素,一个设置了android:layout_weight=“1”,另一个设置了android:layout_weight=“2”,则第一个子元素占总空间的1/3,而第二个子元素占总空间的2/3。

示例代码如下:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Item 1" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Item 2" />

</LinearLayout>

在上面的示例中,第一个TextView的权重为1,第二个TextView的权重为2,因此第一个TextView占总空间的1/3,第二个TextView占总空间的2/3。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门