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

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)

时间: 2024-08-13 01:25:11

android--LinearLayout的child中layout_weight的作用与使用的相关文章

Android:LinearLayout布局中Layout_weight的深刻理解

首先看一下LinearLayout布局中Layout_weight属性的作用:它是用来分配属于空间的一个属性,你可以设置他的权重.很多人不知道剩余空间是个什么概念,下面我先来说说剩余空间. 看下面代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" an

【转】android中layout_weight的理解

android Layout_weight的理解 SDK中的解释: Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated 

Android技术14:Android中layout_weight属性解析

为了更好的对空间进行布局,在LinearLayout中使用layout_weight,然后对于这一属性,在有些书上或者Android的初学者直接认为layout_weight值越大,控件权重就越大,所占用的空间就越大或者layout_wight值越小,控件空间就越大.这两种都是片面的,没有真正认识到layout_weight含义以及如何布局.下面首先演示使用代码为什么会有这两种感觉. 1.演示权重成反比 LinearLayout设置水平布局,然后里面空间宽度为fill_parent,layout

Android LinearLayout的android:layout_weight属性

本文主要介绍Android LinearLayout的android:layout_weight属性意义 android:layout_weight为大小权重,相当于在页面上显示的百分比,它的计算是根据LinearLayout中所有相关元素的此属性值计算的. 除了已经固定大小的,其他设置了此属性的view所占大小(长度或高度)为自己layout_weight属性值/所有layout_weight属性值*总大小.这个属性在android的sdk中都没有介绍.下面举例介绍下 比如在一个layout中

安卓开发技巧一:深入理解Android布局中Layout_weight的属性

今天开始将要为大家介绍一些安卓开发过程将要用到的一些技巧,这些技巧全部来自网络搜集,或者自己在企业做项目的时候总结出来的,利用这些技巧将会对我们开发带来非常方便的便捷性. 先来记录一下这一段时间的技巧目录,方便大家以后方便查阅(大概有不到三十种的技巧总结,大概每周分享两个技巧,笔者将尽可能写的详细,以及提供实例源码): 安卓开发技巧一:深入理解Android布局中Layout_weight的属性 安卓开发技巧二:自定义日志工具类 安卓开发技巧三:Activity的启动模式 安卓开发技巧四:分享一

Android在layout xml中使用include

在Android的layout样式定义中,可以使用xml文件方便的实现,有时候为了模块的复用,使用include标签可以达到此目的.例如: <include layout="@layout/otherlayout"></div> Android开发的官方网站的说明在这里. 其中,有提到: Similarly, you can override all the layout parameters. This means that any android:layou

【Android】一个activity中垂直排列两个listview

为了实现这种效果,一开始我想到的办法是ilistVew中加上一个footerview,footerview里面嵌套一个listview,但是实际操作之后发现footerview里的listview只显示一项,这个问题困扰了半天,一直没有找到合适的解决办法,直到昨天晚上偶然看到一篇博文介绍说,scrollview里嵌套listview也出现了同样的问题,解决办法是动态设置listview的高度,于是我用这种方法也试了试,结果还真解决了. MainActivity.java package com.

Android在layout xml中使用include完成静态加载

Android在layout xml中使用include完成静态加载 include静态加载:不仅可以加载布局,还可以加载控件(控件标签名要在最外层)include标签中有个layout属性就是专门用来加载的. 在Android的layout样式定义中,可以使用xml文件方便的实现,有时候为了模块的复用,使用include标签可以达到此目的.例如: <include layout="@layout/otherlayout"></div> android开发的官方

Android开发之ListView中BaseAdapter的使用

BaseAdapter,官网链接--http://developer.android.com/intl/zh-cn/reference/android/widget/BaseAdapter.html 继承:Object 接口:ListAdapter  SpinnerAdapter 已知直接子类: ArrayAdapter<T>, CursorAdapter, SimpleAdapter 已知间接子类: ResourceCursorAdapter, SimpleCursorAdapter 使用B