剖析Android 线性布局中的权重(layout_weight)(解答疑惑)

http://blog.csdn.net/zhoujn90/article/details/45055863


首先看一下奇怪的的现象:

线性布局的情况下,有个非常奇怪的属性——Android:layout_weight,该属性大部分视图控件中都有,它表示视图的重要度或者权重,看看以下两种情况下该属性的使用:

(1)水平布局的情况下:(android:orientation="horizontal")

第一种情况:设置 android: layout_width="fill_parent" 。

这个时候设置第一个TextView和第二个TextView的layout_weight的值分别为1和2,那么在水平方向上第一个TextView占据了三分之二的宽度,而第二个占据了三分之一的宽度。 也就是在这种情况下,layout_weight的值越大,重要度越低,也就是说占据的宽度越短(不太明白为什么叫重要度,而且这样描述也不太合理,姑且叫他重要度吧)。

第二种情况:设置android: layout_width="wrap_content"。

两个TextView的layout_weight同样是1和2,此时第一个TextView占了三分之一的宽度,而第二个TextView却占用了三分之二,跟上面的情况刚好相反。

(2)垂直布局的情况下:(android:orientation="vertical")

第一种情况:设置android:layout_height="fill_parent"。

设置android:layout_height="fill_parent",两个TextView的设置如下图所示,第一个TextView的高度占了三分之二,而第二个TextView只占了三分一。

第二种情况:设置android:layout_height="wrap_content"。

两个TextView的android:layout_height设置均为wrap_content,第一个TextView的高度占了三分之一,而第二个TextView占了三分之二。(图片就不贴了)

 

总结

根据上面的四个不同场景,可以知道官方说:“layout_weight数值越小,其重要度越高,即占的宽度或者高度份额越大”,可见它是已fill_parent为基准的。

我们看到上面现象一定会有疑问,为什么会出现相反的情况呢!这个使用layout_weight常会遇到的问题。

下面我们看一下“专家”是怎么分析其原理的:


什么是权重(layout_weight)?
通俗地讲,权重(layout_weight)就是对线性布局指定方向(水平或垂直)上剩余空间分配的一个规则。
● 案例分析:
为了便于大家更好地理解权重(layout_weight),接下来,我通过几个案例来分析如何使用权重(layout_weight)对线性布局中水平方向的剩余空间进行分配。
注:以下案例中的测试手机分辨率为480*320,屏幕像素密度为mdpi,即1dp = 1px。
案例一:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" <="" div="" style="word-wrap: break-word; outline: none;">
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >
           android:layout_width="0dp"
<textview< div="" style="word-wrap: break-word; outline: none;">
        android:layout_height="120dp"
        android:layout_weight="3"
        android:background="@android:color/black"/>
           android:layout_width="0dp"<textview< div="" style="word-wrap: break-word; outline: none;">
        android:layout_height="120dp"
        android:layout_weight="1"
      android:background="@android:color/holo_green_dark"/>
图1-1布局效果

当前布局效果如图1-1所示。
从图1-1可以看出,黑色部分的宽度是240像素,绿色部分的宽度是80像素,这两部分所占区域宽度的计算方式如下所示:
当前屏幕横屏宽度:320dp
第一个子控件未分配权重前所占宽度:0dp 
第二个子控件未分配权重前所占宽度:0dp 
当前屏幕剩余空间总数:320dp-0dp-0dp = 320dp,将当前320dp按权重分配给两个子控件,子控件一分配到四分之三,子控件二分配到四分之一
第一个子控件分配权重后宽度:0dp+((320dp-0dp-0dp)*3)/4 = 240dp
第二个子控件分配权重后宽度:0dp+(320dp-0dp-0dp)/4 = 80dp

案例二
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" <="" div="" style="word-wrap: break-word; outline: none;">
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
tools:context=".MainActivity" >
           android:layout_width="60dp"<textview< div="" style="word-wrap: break-word; outline: none;">
        android:layout_height="120dp"
        android:layout_weight="3"
        android:background="@android:color/black"/>
   <textview< div="" style="word-wrap: break-word; outline: none;">
        android:layout_width="60dp"
        android:layout_height="120dp"
        android:layout_weight="1"
     android:background="@android:color/holo_green_dark"/>
当前布局效果如图1-2所示。

图1-2布局效果
从图1-2可以看出,黑色部分的宽度是210像素,绿色部分的宽度是110像素,这两部分所占区域宽度的计算方式如下所示:
当前屏幕横屏宽度:320dp
第一个子控件未分配权重前所占宽度:60dp 
第二个子控件未分配权重前所占宽度:60dp 
当前屏幕剩余空间总数:320dp-60dp-60dp = 200dp,将当前200dp按权重分配给两个子控件,子控件一分配到四分之三,子控件二分配到四分之一
第一个子控件分配权重后宽度:60dp+((320dp-60dp-60dp)*3)/4 = 210dp
第二个子控件分配权重后宽度:60dp+(320dp-60dp-60dp)/4 = 110dp
 案例三 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" <="" div="" style="word-wrap: break-word; outline: none;">
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >
           android:layout_width="260dp"<textview< div="" style="word-wrap: break-word; outline: none;">
        android:layout_height="120dp"
        android:layout_weight="3"
        android:background="@android:color/black"/>
           android:layout_width="260dp"<textview< div="" style="word-wrap: break-word; outline: none;">
        android:layout_height="120dp"
        android:layout_weight="1"
        android:background="@android:color/holo_green_dark"/>

当前布局效果如图1-3所示。
从图1-3可以看出,黑色部分的宽度是110像素,绿色部分的宽度是210像素,这两部分所占区域宽度的计算方式如下所示:
当前屏幕横屏宽度:320dp
第一个子控件未分配权重前所占宽度:260dp 
第二个子控件未分配权重前所占宽度:260dp 
当前屏幕剩余空间总数:320dp-260dp-260dp = -200dp,将当前-200dp按权重分配给两个子控件,子控件一分配到四分之三,子控件二分配到四分之一
第一个子控件分配权重后宽度:260dp+((320dp-260dp-260dp)*3)/4 = 110dp
第二个子控件分配权重后宽度:260dp+(320dp-260dp-260dp)/4 = 210dp
 案例四 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" <="" div="" style="word-wrap: break-word; outline: none;">
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >
           android:layout_width="fill_parent"<textview< div="" style="word-wrap: break-word; outline: none;">
        android:layout_height="120dp"
        android:layout_weight="3"
        android:background="@android:color/black"/>
           android:layout_width="fill_parent"<textview< div="" style="word-wrap: break-word; outline: none;">
        android:layout_height="120dp"
        android:layout_weight="1"
        android:background="@android:color/holo_green_dark"/>
当前布局效果如图1-4所示。

从图1-4可以看出,黑色部分的宽度是80像素,绿色部分的宽度是240像素,这两部分所占区域宽度的计算方式如下所示:
当前屏幕横屏宽度:320dp
第一个子控件未分配权重前所占宽度:fill_parent 即为充满横屏
第二个子控件未分配权重前所占宽度:fill_parent 即为充满横屏 
当前屏幕剩余空间总数:320dp-320dp-320dp = -320dp,将当前-320dp按权重分配给两个子控件,子控件一分配到四分之三,子控件二分配到四分之一
第一个子控件分配权重后宽度:320dp+((320dp-320dp-320dp)*3)/4 = 80dp
第二个子控件分配权重后宽度:320dp+(320dp-320dp-320dp)/4 = 240dp
 案例总结
从上述案例可以看出,如果对线性布局中的控件设置了权重(layout_weight),那么控件占用的空间大小是可以计算出来的,计算公式如下:
线性布局中子控件最终占用宽度 = 原有宽度+剩余空间分配量
例如,在水平方向上的线性布局LinearLayout控件L中,包含两个水平占用空间的控件A、B,其中:
L控件:L控件宽度layout_width = width_l
A控件:A控件宽度layout_width = width_a   A控件权重layout_weight = weight_a
B控件:B控件宽度layout_width = width_b   B控件权重layout_weight = weight_b
L中子控件最终占用宽度 = 原有宽度(width_a)+剩余空间分配量
A所占宽度 = width_a + (width_l-width_a-width_b)*weight_a/(weight_a+weight_b)
B所占宽度 = width_b + (width_l-width_a-width_b)*weight_b/(weight_a+weight_b)
由此可以推断,当使用权重(layout_weight)时,会遇到下列两种情况:
情况1:当L中内部子控件(A,B)的宽度之和大于L的总宽度时,即(width_l-width_a-width_b)<0时,weight_a/(weight_a+weight_b)比例的值越大,当前控件所占空间越小。
情况2:当L中内部子控件(A,B)的宽度之和小于L的总宽度时,即(width_l-width_a-width_b)>0时,weight_a/(weight_a+weight_b)比例的值越大,当前控件所占空间越大。

我们在使用的时候一般情况把要布局的一般吧width或者height设置为0dp


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答
以下代码有无错误 <?xml version="1.0" encoding="utf-8"?> <com.blog.demo11.DragViewGroup xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="70dp" android:layout_height="70dp" android:text="@string/txt_drag" android:gravity="center" android:textColor="#fff" android:background="#6495ED" android:layout_margin="24dp" /> </com.blog.demo11.DragViewGroup> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/backgroundImage" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/tianzige" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:orientation="horizontal"> <ImageView android:id="@+id/image1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/gengzhegou" /> <ImageView android:id="@+id/image2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pie" android:layout_marginLeft="10dp" /> <ImageView android:id="@+id/image3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/shuwangou" android:layout_marginLeft="10dp" /> </LinearLayout> </RelativeLayout>
06-04

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值