android控件weight

android屏幕适配中有一项比较常见的使用方式是使用android:layout_weight,按比例分配,大家都很常用,这里只mark一下计算的规则

权重控件的使用必须在LinearLayout中使用,先看一下不同属性下的不同效果

1.当权重属性为match_parent的时候

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"   //注意这一块
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/red"
        />
    <TextView
        android:layout_width="match_parent"   //<-----
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="@color/green"
        />
</LinearLayout>
效果如下:


2.当属性为wrap_content的时候
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/red"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="@color/green"
        />
</LinearLayout>


3.当属性值为0的时候
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/red"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="@color/green"
        />
</LinearLayout>


基本上每个android开发者都会用,这里记一下它的计算规则
        显示大小 = 控件的大小+剩余的大小 * 权重的比值
      如:2/3L   =   L  + (L -  2L)*1/3    
注:假设屏幕的宽度是L
纵向的weight的使用方式也是如此
打完收工




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值