android:weight的使用

本文详细介绍了在Android布局中使用weight属性实现控件按比例显示的方法,并通过实例展示了如何调整宽度比例来控制控件的显示效果。通过将weight属性应用于不同宽度设置,可以灵活地调整控件在屏幕上的布局比例,实现美观且响应式的UI设计。

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

android:weight属性能够根据手机高宽按设定比例显示控件
如:
宽度上按比例显示
<LinearLayout
                android:layout_marginTop="5dp"
                androidrientation="horizontal"
                android:layout_width="fill_parent"----------------让其满宽
                android:layout_height="wrap_content">
            <Button
                    android:id="@+id/first_sex1_4"
                    android:layout_marginLeft="15dp"
                    android:layout_width="0dp"---------设置0dp,让宽度由比例生成
                    android:layout_weight="1"
                    android:layout_height="50dp"/>
            <Button
                    android:id="@+id/first_sex1_5"
                    android:layout_marginLeft="5dp"
                   android:layout_width="0dp"
                    android:layout_weight="3"
                    android:layout_height="50dp"/>         
        </LinearLayout>
注意现在android:layout_width="0dp"
最终3个按钮显示的宽度比例为1:3,和weight一样,值大的权高,显示宽度大

然而如果把 android:layout_width="fill_parent"
<LinearLayout
                android:layout_marginTop="5dp"
                androidrientation="horizontal"
                android:layout_width="fill_parent"----------------让其满宽
                android:layout_height="wrap_content">
            <Button
                    android:id="@+id/first_sex1_4"
                    android:layout_marginLeft="15dp"
                    android:layout_width="fill_parent"---------设置fill_parent,让宽度由比例生成
                    android:layout_weight="1"
                    android:layout_height="50dp"/>
            <Button
                    android:id="@+id/first_sex1_5"
                    android:layout_marginLeft="5dp"
                   android:layout_width="fill_parent"
                    android:layout_weight="3"
                    android:layout_height="50dp"/>         
        </LinearLayout>
注意现在android:layout_width="fill_parent"
最终3个按钮显示的宽度比例为3:1,和weight相反,值大的权低,显示宽度小
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值