android:layout_weight 用法解释

1.  layout_weight默认值为0


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#f0f"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:text="@string/hello_world" />

</LinearLayout>


第一个text1 layout_weight为1  其他默认为0, LinearLayout orientation=“vertical”

最终效果是text2 text3高度正常  text1占据整个屏幕剩下的高度,

text1 layout_height=“0dp” 是因为只有一个组件设置了layout_weight


若这个三个text的weight分别为 1,2,3

则整个屏幕高度被分为1:2:3的三个区域


值得注意的是 若比例是1:10000之类的大比值  显示并不是完全按照这个比值 小的比值内容还是会显示出来  有个最小高度



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值