layout_weight如何计算比例?

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-31 23:53   30   0


一。当LinearLayout中的Button的layout_width="wrap_content"时

一切没问题,也很容易理解,btn1和btn2和btn3宽度的比就是1:2:3也就是weight的比


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    
    <Button
        android:id="@+id/btn1"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:text="1">
    </Button>
    <Button 
        android:id="@+id/btn2"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:layout_weight="2"
        android:text="2">
    </Button>
     <Button 
        android:id="@+id/btn3"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:layout_weight="3"
        android:text="3">
    </Button>
</LinearLayout>



当然如果你的Button的内容太长了,长得你的weight=1满足不了“layout_width=“”wrap_content”就会出现这样:


  
    <Button
        android:id="@+id/btn1"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:text="11111111111111111111111">


解决方案:将button属性改为layout_width=”0dp“,原因是layout_height和layout_width的优先级大于layout_weight,如果layout_width="wrap_content",button就会去先满足wrap_content!~~~但你layout_width=”0dp“时不设置weigth时会报错的:Suspicious size: this will make the view invisible, should be used with layout_weight


  
    <Button
        android:id="@+id/btn1"
        android:layout_height="fill_parent"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:text="11111111111111111111111">
    </Button>


二。当LinearLayout中的Button的layout_width="fill_parent"时


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    
    <Button
        android:id="@+id/btn1"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:text="1">
    </Button>
    <Button 
        android:id="@+id/btn2"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="2"
        android:text="2">
    </Button>
     <Button 
        android:id="@+id/btn3"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="3"
        android:text="3">
    </Button>
</LinearLayout>



咦?btn3哪去了?来看看weight的计算公式吧,因为btn1、2、3都是layout_width=”fill_parent“,那么就少了2个parent_width啊~~(如果没有这weight,这时应是btn1布满屏幕),"之前的如果分配为fill_parent的话把,剩余的为1个parent_width (parent_width指的是屏幕宽度 )-3个parent_width=(-2个parent_width ),再按比例分配 :btn1所占空间=1个parent_width+1/6 *(-2个parent_width)=(2/3个parent_width)

btn2:1个parent_width+2/6 *(-2个parent_width)=(1/3个parent_width)"


这样一来btn1和btn2就已经占了2/3+1/3=1个parent_width了,所以就看不见btn3了

改变一下weight的比,,再看

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    
    <Button
        android:id="@+id/btn1"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="2"
        android:text="1">
    </Button>
    <Button 
        android:id="@+id/btn2"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="3"
        android:text="2">
    </Button>
     <Button 
        android:id="@+id/btn3"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="4"
        android:text="3">
    </Button>
</LinearLayout>

恩恩,就是这样,
总之:1.layout_width=”wrap_content“时,按照weight的比例显示(内容过长时--->layout_width=”0dp“)
            2.layout_width=”fill_parent“时,按照上面的公式算算比例就行

严重参考过的文章
点击打开链接

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP