LinearLayout的layout_weight属性与视图分配
来源:互联网 发布:网络主播收入排行 编辑:程序博客网 时间:2024/06/05 05:18
LinearLayout的layout_weight属性与视图分配
在Android中,LinearLayout几乎是使用得最多的一种布局。
而在LinearLayout中,layout_weight属性又是很多同学没有理解的。
android:layout_weight属性用来进行LinearLayout的组件布局安排。如果我们为同一个LinearLayout中的两个组件设置了相同的android:layout_weight值,这并不意味着他们在屏幕中有相同的高度或者宽度(依布局方向而定)。
要理解这些,首先要清楚,在LinearLayout中,视图宽度是分成两个步骤来分配的。
第一步, LinearLayout查看layout_width或者layout_height的值(依布局方向而定),一个很简单的例子,在一个LinearLayout中,有两个组件,当这两个组件的layout_width都设置为wrap_content时,他们获得的空间仅能绘制自身。代码如下。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="left" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:text="@string/hello_world" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /></LinearLayout>
实际效果如下。
可以看到整个横部还有剩余的空间。
第二步,LinearLayout依据layout_weight属性进行额外的空间分配。
如果我们给他们赋以相同的layout_weight属性值,代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="left" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:layout_weight="1" android:text="@string/hello_world" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/hello_world" /></LinearLayout>
而效果是这样的:
可以看到,虽然两个的layout_weight属性值都一样,但是他们分配到的空间却是不一样的,原因是LinearLayout在第一步已经分配过空间,第二步只是将剩余的空间分配。事实上,第一步中剩余的空间,在这一步中被平均分配了。
那么问题来了,如果我想这两个控件平均分配整个空间而不是剩余空间,该怎么做?
很简单,取消掉LinearLayout的第一步空间分配,只进行第二步分配。
- 1:1分配
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="left" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="30sp" android:layout_weight="1" android:text="@string/hello_world" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/hello_world" /></LinearLayout>
- 1:2分配
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="left" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="20sp" android:layout_weight="1" android:text="@string/hello_world" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="@string/hello_world" /></LinearLayout>
可以看到,如果将layout_width属性值(垂直布局时是layout_height)设置为0dp,那么就可以跳过LinearLayout的第一步空间分配,从而轻松实现一定的宽度比。
- LinearLayout的layout_weight属性与视图分配
- LinearLayout的layout_weight属性
- LinearLayout layout_weight属性的妙用
- LinearLayout布局的layout_weight属性
- LinearLayout 中 layout_weight 与weightSum属性研究
- Android 布局 LinearLayout 的 layout_weight 属性
- LinearLayout中的layout_weight属性 的使用
- Android LinearLayout布局的layout_weight属性探究
- linearlayout布局的属性 gravity layout_gravity layout_weight
- LinearLayout中的layout_weight属性
- LinearLayout中的layout_weight属性
- LinearLayout中的layout_weight属性
- LinearLayout中layout_weight属性
- LinearLayout中的layout_weight属性
- LinearLayout中的layout_weight属性
- LinearLayout中的layout_weight属性
- LinearLayout中layout_weight属性
- 关于Android UI组件LinearLayout属性layout_weight与layout_width/height的问题
- PHP漏洞之session会话劫持
- java 多线程
- sql中奇怪的sum(1),sum(2),count(1),count(6),count(*):统计总数
- Hello World!第一个 OSGi 应用程序
- PHP数据缓存技术
- LinearLayout的layout_weight属性与视图分配
- Linux下安装openldap 启动及配置一站式搞定
- findViewById的快捷工具类
- 【2015.逃离华为之找工作前期准备】
- Thrift 网络通信库 编译部署小结
- Yii2 实现上下联动的下拉框
- g++/ld中的符号重定义
- test
- A 57x57 app icon is required for iPhone apps targeting releases of iOS prior to 7.0