android:layout_weight是否仅用于LinearLayout,还是也可以在RelativeLayout中使用它?

问题描述 投票:1回答:3

如果android:layout_weight仅用于LinearLayout,为什么?

android android-linearlayout android-relativelayout android-layout-weight
3个回答
1
投票

读取Layout Weight

此属性根据视图应在屏幕上占据多少空间为其分配“重要性”值。较大的权重值使其可以扩展以填充父视图中的所有剩余空间。子视图可以指定一个权重值,然后视图组中的所有剩余空间将按照其声明的权重比例分配给子视图。默认权重为零。

一种相对布局显示其相对于彼此的视图,因此顺序并不那么重要。


1
投票

android:layout_weight是LinearLayout的属性,它继承了此属性给它的子对象,因此,在您的情况下,如果您的孩子是RelativeLayout,那么我们可以将android:layout_weight与RelativeLayout一起使用。

请参见下面的示例

<androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_centerInParent="true">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

        </androidx.appcompat.widget.LinearLayoutCompat>

这就是它>>


0
投票

RelativeLayout没有权重属性,但是ConstraintLayout具有权重属性,例如LinearLayout。

© www.soinside.com 2019 - 2024. All rights reserved.