Android中的android:layout_weight使⽤详解
在使⽤LinearLayout的时候,⼦控件可以设置layout_weight。layout_weight的作⽤是设置⼦空间在LinearLayout的重要度(控件的⼤⼩⽐重)。layout_weight的值越低,则控件越重要。若不设置layout_weight则默认⽐重为0。
如果在⼀个LinearLayout⾥⾯放置两个Button,Button1和Button2,Button1的layout_weight设置为1,Button2的
layout_weight设置为2,且两个Button的layout_width都设置为fill_parent。
复制代码代码如下:weight的固定搭配
<LinearLayout xmlns:android="schemas.android/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2"/>
</LinearLayout>
则Button1占据屏幕宽度的三分之⼆,⽽Button2占据三分之⼀,如下图所⽰:
如果两个Button的layout_width都设置成wrap_content,则情况刚好相反。Button1占三分之⼀,Button2占三分之⼆,如下图所⽰:
layout_weight在使⽤LinearLayout设计复杂的布局时还是挺有⽤处的,例如,在⽔平的线性布局中,你要分⾜够的空间给控件1,剩下的空间则分配给控件2,则只要设置控件1的layout_width设置为wrap_content,不⽤设置layout_weight,⽽在控件2中,设置layout_width为fill_parent,layout_weight为1即可实现。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。