android:layout_weight深入理解
来源:互联网 发布:股票行情数据接口整理 编辑:程序博客网 时间:2024/06/11 12:30
1 :android:layout_weight 属性理解
在Linerlayout中有 android:layout_weight这个属性,是给子view分配权重,现在我们来设置三个TextView,权重分别设为1,2,2:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:baselineAligned="false" > <TextView android:layout_width="match_parent" android:layout_height="50dp" android:layout_weight="1" android:gravity="center" android:textColor="#ffffffff" android:background="@color/colorPrimaryDark" android:text="Hello World 1 " /> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:layout_weight="2" android:background="@color/colorAccent" android:gravity="center" android:text="Hello World 2" /> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:background="#ff7a61" android:layout_weight="2" android:text="Hello World 3" android:gravity="center" /></LinearLayout>
1:2:2按比例分配,按照我们的理解应该是这样的:图1
可是实际上却是这样的:图2
纳尼?这是怎么回事?
请看这句话: Specifies how much of the extra space in the layout to be allocated to a view.
android:layout_weigh 首先按照控件声明的尺寸进行分配,然后再将剩下的尺寸按weight分配,最终分配的大小就是 view控件宽度+剩余父控件宽度*weight
这里我们match_parent ,假设屏幕宽度500dp,那么Hello world 1 分配的宽度= 500(控件宽度) + (500-3*500)/5 = 3*500/5 就是如图2所示。占了整个屏幕的3/5(这里500-3*500 是剩余父控件宽度,他等于屏幕宽度-子view宽度之和),同理Hello world2,3 ,就分别占总屏幕的1/5:
所以正确的布局写法应该是:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:baselineAligned="false" > <TextView android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:gravity="center" android:textColor="#ffffffff" android:background="@color/colorPrimaryDark" android:text="Hello World 1 " /> <TextView android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="2" android:background="@color/colorAccent" android:gravity="center" android:text="Hello World 2" /> <TextView android:layout_width="0dp" android:layout_height="50dp" android:background="#ff7a61" android:layout_weight="2" android:text="Hello World 3" android:gravity="center" /></LinearLayout>对宽width 分配就把width 设置0dp ,对高Hight分配就把hight设置0dp,这样就能得到图1的效果。
2 :android:layout_weight 属性妙用
在之前写过一篇高仿QQ主界面,主界面是Framlayout+底部栏:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_weight="1" android:layout_height="0dp"> </FrameLayout> <include layout="@layout/bottom"/> </LinearLayout>
对FrameLayout 设置weight属性,对底部不设置。那么最终add到Framelayout 的Fragment将被分配除底部栏以外的所有空间,这种带底部栏的布局,以后都可以这样写,不需要用Relativelayout 布局,消耗更多的性能。
0 0
- android:layout_weight深入理解
- 深入理解Layout_weight
- 深入理解layout_weight属性
- android layout_weight的理解
- Android:Layout_weight理解
- Android layout_weight理解
- android:layout_weight 深刻理解
- Android:Layout_weight的理解
- android:layout_weight的理解
- Android layout_weight理解
- Android layout_weight 理解
- android 深入了解layout_weight属性
- Android layout layout_weight属性理解
- android中layout_weight的理解
- android中layout_weight的理解
- android中layout_weight的理解
- Android:Layout_weight的深刻理解
- android中layout_weight的理解
- Eclipse常用快捷键
- Linux 上的基础网络设备详解
- Android5.x上运行ARM架构程序
- [李景山php] 深入理解PHP内核[读书笔记]--第三章:变量及数据类型--变量的结构和类型--静态变量
- GCC 库顺序问题解决方法(转)
- android:layout_weight深入理解
- Struts2验证(声明式验证、自定义验证器)
- 一天搞定CSS:支持IE的Layout布局--16
- 关于java多线程浅析二:线程的状态
- BeagleBone Black编程学习_eclipse C/C++
- 理解Joomla!模板
- bzoj3450 Easy
- 腾讯云ubuntu下载tomcat+配置+项目发布
- Android面试总结----搬运工也有梦想