Android中的android:layout_weight使用详解
更新时间:2013年06月16日 16:20:07 作者:
layout_weight的作用是设置子空间在LinearLayout的重要度(控件的大小比重)。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。
<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即可实现。
如果在一个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即可实现。
您可能感兴趣的文章:
- jQuery布局插件UI Layout简介及使用方法
- android layout 按比例布局的代码
- 基于AnDroid FrameLayout的使用详解
- jQuery EasyUI 中文API Layout(Tabs)
- SWT(JFace)体验之GridLayout布局
- android LinearLayout和RelativeLayout组合实现精确布局方法介绍
- jqeury-easyui-layout问题解决方法
- Android布局——Preference自定义layout的方法
- Android开发笔记 TableLayout常用的属性介绍
- CoordinatorLayout的使用如此简单(Android)
相关文章
Android中RecyclerView实现分页滚动的方法详解
RecyclerView实现滚动相信对大家来说都不陌生,但是本文主要给大家介绍了利用Android中RecyclerView实现分页滚动的思路和方法,可以实现翻页功能,一次翻一页,也可以实现翻至某一页功能。文中给出了详细的示例代码,需要的朋友可以参考借鉴,下面来一起看看吧。2017-04-04Android Studio控制台出现中文乱码(方框)问题解决办法
这篇文章主要介绍了Android Studio控制台出现中文乱码(方框)问题解决办法的相关资料,需要的朋友可以参考下2017-06-06Android日期选择器对话框DatePickerDialog使用详解
这篇文章主要为大家详细介绍了Android日期选择器对话框DatePickerDialog的使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2018-01-01Android AndBase框架使用封装好的函数完成Http请求(三)
这篇文章主要介绍了Android AndBase框架使用封装好的函数完成Http请求的相关资料,感兴趣的小伙伴们可以参考一下2016-03-03Android关于BottomNavigationView使用指南
本文主要介绍了Android关于BottomNavigationView使用指南,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2022-01-01
最新评论