android--LinearLayout的child中layout_weight的作用与使用

摘要:
子对象的宽度(layout_width)可分为三种类型:_权重参数共同决定两个子对象的布局_权重分别为weight_1和weight_2(layout_weight参数为int值,layout_weight分别为1和21<layout_height=“wrap_content”12android:

linearLayout中包含有weight的child时,linearLayout会measure两次:

第一次 测量 child 的 原始值:

第二次 测量 child 的 比重值:

然后将2次测量的值相加,得到child 的具体的宽 或 高。

//-----------------------------------------------------------------------------

我们假设 一个LinearLayout方向为横向android:orientation="horizontal" ,它有2个child,分别为 child_1 和 child_2。

我们来分析下 这2个child的宽度在带有layout_weight属性的情况下,值是怎么计算的。

第一次测量时:有自己的layout_width参数决定

首先,child的宽度(layout_width)有3种情况:0dp(具体值,可以随意设置,例如 100dp)、wrap_content、match_parent。

这3种情况,当应用加载到具体的手机上时,都会在测量时,变为具体的值,分别如下:

1,当child_1 的 layout_width 为 0dp,它的width_orginal 原始宽度为0pix;(100dp,则 width_orginal 为 屏幕密度density * 100 pix = X_1 pix)

  (其中手机屏幕密度 通过 context.getResources().getDisplayMetrics().density 获取)

2,当child_1 的 layout_width 为 wrap_content,它的width_orginal 原始宽度为 int_wrap_cotent(在具体的手机上是一个固定值,假设为 X_1  pix) ;

3,当child_1 的 layout_width 为 match_parent,它的width_orginal 原始宽度为手机屏幕的宽度(在具体的手机上是一个固定值,假设为 X_1 pix) ;

(child_2 的 原始宽度为  X_2)

第二次测量时:由所有的child 的 layout_weight 参数共同决定

假设 2个child的 layout_weight 分别为 weight_1 和 weight_2 (layout_weight 参数 是一个int类型值,可以为0):

那么 child_1 和 child_2 的最终宽度为:

child_1的宽度:X_1 + (手机屏幕宽度 - (X_1 + X_2))  * weight_1 / (weight_1+weight_2) = 最终宽度。

以下是几个例子:

demo1:child_1 和 child_2 的layout_width都为0dp,layout_weight分别为 1 和 2

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/root"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="horizontal" >
 7 
 8     <TextView
 9         android:id="@+id/tv_left"
10         android:layout_width="0dp"
11         android:layout_height="wrap_content"
12         android:layout_weight="1"
13         android:background="#00ff00"
15         android:text="child_1"
16         android:textSize="24sp" />
17 
18     <TextView
19         android:id="@+id/tv_right"
20         android:layout_width="0dp"
21         android:layout_height="wrap_content"
22         android:layout_weight="2"
23         android:background="#ff0000"
24 
25         android:text="child_2"
26         android:textSize="24sp" />
27 
28 </LinearLayout>

此时,child_1 的最终宽度为:0 pix + 屏幕宽度 * 1/3 = 1/3 的屏幕宽度。child_2 最终宽度为 2/3 的屏幕宽度。

所以想要按比例分配LinearLayout的children的宽(高)值,可以用次方法。

demo2:child_1 和 child_2 的layout_width都为100dp,layout_weight分别为 1 和 2。

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/root"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="horizontal" >
 7 
 8     <TextView
 9         android:id="@+id/tv_left"
10         android:layout_width="100dp"
11         android:layout_height="wrap_content"
12         android:layout_weight="1"
13         android:background="#00ff00"
14 
15         android:text="child_1"
16         android:textSize="24sp" />
17 
18     <TextView
19         android:id="@+id/tv_right"
20         android:layout_width="100dp"
21         android:layout_height="wrap_content"
22         android:layout_weight="2"
23         android:background="#ff0000"
24 
25         android:text="child_2"
26         android:textSize="24sp" />
27 
28 </LinearLayout>

此时,child_1 的最终宽度为:100 * density pix + (屏幕宽度 - 200 dp * density) * 1/3 = 。。。

demo2:child_1 和 child_2 的layout_width都为match_parent,layout_weight分别为 1 和 2。

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/root"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="horizontal" >
 7 
 8     <TextView
 9         android:id="@+id/tv_left"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content"
12         android:layout_weight="1"
13         android:background="#00ff00"
14 
15         android:text="child_1"
16         android:textSize="24sp" />
17 
18     <TextView
19         android:id="@+id/tv_right"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:layout_weight="2"
23         android:background="#ff0000"
24 
25         android:text="child_2"
26         android:textSize="24sp" />
27 
28 </LinearLayout>

此时,child_1 的最终宽度为:屏幕宽度 + (屏幕宽度 - 2倍的屏幕宽度) * 1/3 = 2/3 的 屏幕宽度,child_2为 1/3 的屏幕宽度。

当 child 的 layout_width 都为 match_parent时,想要 child_1 : child_2 = 1 : 2(即 1/3 : 2/3),可以设置 child_1 child_2 的 layout_weight 为 (分母 - 1) : (分母 - 2).

当 child 的 layout_width 都为 match_parent时,想要child_1 : child_2 : child_3 = 3 : 4 : 5 (即 3/12 : 4/12: 5/12),可以设置layout_weight 分别为 (12-3):(12-4):(12-5)

免责声明:文章转载自《android--LinearLayout的child中layout_weight的作用与使用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇闲话 笔记本显卡性能排行洛谷P1282 多米诺骨牌 (DP)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android常用工具之Bugly的使用

bugly是什么?百度百科上对bugly的介绍 总结下来是:腾讯的开放给开发者的一种平台服务,主要用于android和ios平台上的移动应用的crash和卡顿检测和快速定位以及提供解决方案。是免费服务。 而用过的人都知道,除了crash检测外,bugly还提供应用内升级和热修复等功能。 一切以官方文档为准:https://bugly.qq.com/docs...

用HTML5/CSS3/JS开发Android/IOS应用

现在人人都想成为安卓/IOS应用开发工程师。其实,安卓/IOS应用可以用很多种语言来实现。由于我们前端开发工程师,对HTML5/CSS/JavaScript的网络编程已经相当熟悉了。所以,今天大家将会认识到一些利用前端语言来开发安卓/IOS应用的工具。  在文章的末尾,也介绍了使用JAVA、C#、Lua以及AS3来开发安卓应用的工具。  希望大家都能找到适...

第6章 约束满足问题CSP

第六章 约束满足问题CSP 一、问题定义 问题定义 状态空间,状态被定义为在值域Di中的变量Xi; 当前状态; 转移模型; 目标测试,每个变量都有自己的赋值同时满足约束条件; 评估函数。 三个成分 X是变量集合 [{X_1,...,X_n} ] D是值域集合 [{D_1,...,D_n} ] ,每个变量都有自己的值域。 C是描述变量取值的约束集合...

andoird软件开发之一个记录账号密码的APP--bmob后台

1.app功能:通过注册登录账户,拥有一个账户本,能够将平时自己容易的忘记的账户记录下来,并可以保持到云端,不需要担心数据丢失,只要登录账户,便可获取到自己的账户本。 2.实现的效果图,如下: 以下界面分别为注册界面、登录界面、提交账户内容界面、账户列表界面、长按删除账户信息、具体账户内容信息 3.实现的工程目录如下: 4.实现的具体过程: a.布局...

pcap文件格式及文件解析

第一部分:PCAP包文件格式 一 基本格式:    文件头 数据包头数据报数据包头数据报...... 二、文件头:        文件头结构体 sturct pcap_file_header {      DWORD           magic;      DWORD           version_major;      DWORD       ...

document.cookie的使用

设置cookie 每个cookie都是一个名/值对,可以把下面这样一个字符串赋值给document.cookie:document.cookie="userId=828";如果要一次存储多个名/值对,可以使用分号加空格(; )隔开,例如: document.cookie="userId=828; userName=hulk"; 在cookie的名或值中不...