如果android:layout_weight
仅用于LinearLayout
,为什么?
此属性根据视图应在屏幕上占据多少空间为其分配“重要性”值。较大的权重值使其可以扩展以填充父视图中的所有剩余空间。子视图可以指定一个权重值,然后视图组中的所有剩余空间将按照其声明的权重比例分配给子视图。默认权重为零。
一种相对布局显示其相对于彼此的视图,因此顺序并不那么重要。
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>
这就是它>>
RelativeLayout没有权重属性,但是ConstraintLayout具有权重属性,例如LinearLayout。