加入收藏 | 设为首页 | 会员中心 | 我要投稿 开发网_开封站长网 (http://www.0378zz.com/)- 科技、AI行业应用、媒体智能、低代码、办公协同!
当前位置: 首页 > 教程 > 正文

Android中的android:layout_weight分析

发布时间:2021-12-13 12:46:44 所属栏目:教程 来源:互联网
导读:在使用LinearLayout的时候,子控件可以设置layout_weight。layout_weight的作用是设置子空间在LinearLayout的重要度(控件的大小比重)。layout_weight的值越低,则控件越重要。若不设置layout_weight则默认比重为0。 如果在一个LinearLayout里面放置两个Butt
在使用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。
 
<LinearLayout xmlns:Android="http://schemas.android.com/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即可实现。

(编辑:开发网_开封站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读