android:layout_weight总有你不知道的用法

      之前对android:layout_weight的用法大概只知道下面第一种用法,后来在做“类似微信主界面”的时候,遇到了怎样将FrameLayout占据除底部标签栏之外的空间的问题,也就是下文中第二种用法。是的,android:layout_weight总有你不知道的用法,受益不少,先分享之。  

      原文地址:http://www.cnblogs.com/fangyucun/p/3535536.html

      都知道weight是权重的意思. 在布局中起到非常重要的作用. 但是这玩意不能嵌套使用, 而且只能使用在LinearLayout中.

  下面说说它的几种用法(以下例子全为横排 注意android:layout_width值和android:layout_weight值的变化)  

  第一种, 最普遍的-----均分, weight的值越大, 占的空间越大.注意android:layout_width的值都为0dp

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#000" />

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#888" />

</LinearLayout>
复制代码

   第二种, 占满剩余空间, 不管是否处在最后一个,如实例中的Button。

       这就是我之前困惑的地方,用途还是很广的,类似微信主界面都用到了android:layout_weight的该用法。有一点原文未提及:

       当LinearLayout排列方式为horizontal的时候,如例子中一样设置,子组件应设置layout_width="0dp" ,ayout_height="match_parent",layout_weight="1";但当排列方式为vertical的时候,子组件应设置layout_width="match_parent", layout_height="0dp", layout_weight="1",这样就可以保证子组件铺满剩余空间。

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <FrameLayout
        android:layout_width="40dp"
        android:layout_height="match_parent"
        android:background="#000" />

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <FrameLayout
        android:layout_width="40dp"
        android:layout_height="match_parent"
        android:background="#888" />

</LinearLayout>
复制代码

   第三种, 值越大占的空间越小, 值为同布局下其它控件的weight之和时,即消失.注意android:layout_width的值都为match_parent为0时占满. 有什么用途? 呵呵, 存在即是合理的. 

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#000" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.5"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#000" />

</LinearLayout>
复制代码

 

   最后一种(我会用的), 结合android:weightSum使用, 那这儿就有一个问题引入, 如何将控件放在屏幕的中央, 宽度为屏幕的一半?  嗯.....想啊.....想啊...RelativeLayout? 哦, 没法控制? 哦,只能代码了... 

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:weightSum="2">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

</LinearLayout>
复制代码

 

 ok, so easy!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值