android:layout_weight深入理解

 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值