剖析Android线性布局中 Layout_weight(权重)

首先什么是权重?

权重(Layout_weight):通俗的讲,是对线性布局指定方向(水平或垂直)上剩余空间分配的一个规则。

案例分析:为了让大家更好的理解权重,接下来,我举几个栗子来分析如何使用权重对线性布局中水平方向的剩余空间进行分配。
注:以下案例测试手机分辨率320*480,屏幕像素密度为mdpi,即1dp=1px。

案例一:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="120dp"
        android:layout_weight="3"
        android:background="#FF0000"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="120dp"
        android:layout_weight="1"
        android:background="#FFFF00"/>
</LinearLayout>

布局效果图1-1:

从图1-1可以看出红色(#FF0000)宽度是240像素,黄色(#FFFF00)宽度是80像素。

当前屏幕横屏宽度是: 320dp
第一个TextView未分配权重前所占宽度是: 0dp
第二个TextView未分配权重前所占宽度是: 0dp
当前屏幕剩余空间总数: 320dp-0dp-0dp = 320dp,将当前320dp分给两个TextView,第一个占3/4,第二个占1/4。
第一个TextView分配权重后宽度: 0dp+(320dp-0dp-0dp)*(3/4)=240dp
第二个TextView分配权重后宽度: 0dp+(320dp-0dp-0dp)*(1/4)=80dp

案例二:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_weight="3"
        android:background="#FF0000"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_weight="1"
        android:background="#FFFF00"/>
</LinearLayout>

布局效果图1-2:

从图1-2可以看出红色(#FF0000)宽度是80像素,黄色(#FFFF00)宽度是240像素。

当前屏幕横屏宽度是: 320dp
第一个TextView未分配权重前所占宽度是: match_parent,即320dp
第二个TextView未分配权重前所占宽度是: match_parent,即320dp
当前屏幕剩余空间总数: 320dp-320dp-320dp = -320dp,将当前320dp分给两个TextView,第一个占3/4,第二个占1/4。
第一个TextView分配权重后宽度: 320dp+(320dp-320dp-320dp)*(3/4)=80dp
第二个TextView分配权重后宽度: 320dp+(320dp-320dp-320dp)*(1/4)=240dp

从上述案例可以看出,如果对线性布局中的控件设置权重,那么控件占用的空间大小是可以计算出来的,计算公式:

线性布局中子控件最终占用宽度 = 原有宽度+剩余空间分配量

由此可以推断,当使用权重(layout_weight)时,会遇到下列两种情况:
情况1:当L中内部子控件的宽度之和大于L的总宽度时,权重值越大,当前控件所占空间越小。
情况2:当L中内部子控件的宽度之和小于L的总宽度时,权重值越大,当前控件所占空间越大

遗留问题:

  • 一个控件设权重一个没设的情况?
  • 设置一个宽度为wrap_content,一个为match_parent时,布局情况?
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值