Android按比例布局

本文介绍了如何在Android的LinearLayout中通过weight属性实现按比例布局。当子控件的layout_width设为"wrap_content"时,比例可能因内容长度变化而失效。解决方法是将子控件的layout_width设为0dp,配合weight属性,以确保按预设比例分配空间。此外,当layout_width设为"fill_parent"时,可能出现显示不全的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果要实现在LinearLayout里实现按比例布局,一般采取的方式都是用weight来控制。

比如要实现几个控件分别按1:2:3的宽度来布局,父控件一般“layout_width”属性是“fill_parent”,关键是子控件的width和weight该怎么控制。

第一种情况,LinearLayout内的控件的layout_width设置为"wrap_content"

<LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
         >
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="#aa0000"
                    android:gravity="center"
                    android:text="1"/>
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="2"
                    android:background="#00aa00"
                    android:gravity="center"
                    android:text="1"/>
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="3"
                    android:background="#0000aa"
                    android:gravity="center"
                    android:text="1"/>
    </LinearLayout>


可以发现似乎可以实现按1、2、3的比例来控制子空间的宽度比。

但是当第一个textview里面的文本由1变成一长串字符串时,事情发生了变化。设备不会按照我们的比例进行分配宽度。


这个时候我们把子控件"layout_width"属性都设置为0dp,问题就得到了解决。

<LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1">
            <TextView
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="#aa0000"
                    android:gravity="center"
                    android:text="1111111111111111111111111111111111111111111"/>
            <TextView
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="2"
                    android:background="#00aa00"
                    android:gravity="center"
                    android:text="2"/>
            <TextView
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="3"
                    android:background="#0000aa"
                    android:gravity="center"
                    android:text="3"/>
    </LinearLayout>


第二种情况,LinearLayout内的控件的layout_width设置为"fill_parent"

< LinearLayout
             android:orientation ="horizontal"
             android:layout_width ="fill_parent"
             android:layout_height ="fill_parent"
             android:layout_weight ="1" >
             < TextView
                     android:layout_width ="fill_parent"
                     android:layout_height ="fill_parent"
                     android:layout_weight ="1"
                     android:background ="#aa0000"
                     android:gravity ="center"
                     android:text ="1" />
             < TextView
                     android:layout_width ="fill_parent"
                     android:layout_height ="fill_parent"
                     android:layout_weight ="2"
                     android:background ="#00aa00"
                     android:gravity ="center"
                     android:text ="2" />
     </ LinearLayout >

这个时候可以发现两个子空间所占宽度与所设置的"weight"数值成反比。再看看三个色块的效果。占宽度与所设置的发现第三块色块只显示了一部分或者干脆没显示,说明如果用"fill_parent"还是会有一些问题的,就是显示不全的问题。

<LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1">
            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="#aa0000"
                    android:gravity="center"
                    android:text="1"/>
            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="2"
                    android:background="#00aa00"
                    android:gravity="center"
                    android:text="2"/>
            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="3"
                    android:background="#0000aa"
                    android:gravity="center"
                    android:text="3"/>
    </LinearLayout>




所以,按比例显示LinearLayout内各个子控件,需设置android:layout_width="0dp",如果为竖直方向的设置android:layout_height="0dp"。在这种情况下某子个控件占用LinearLayout的比例为:本控件weight值 / LinearLayout内所有控件的weight值的和。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值