android布局中权重(weight)属性的使用

       在android开发中有的时候我们需要我们的布局按照我们需要的比例进行等分,或者我们需要某个控件占用的大小

是除了我们指定的控件占用的大小之外剩下的所有空间。这个时候我们需要用的android:layout_weight属性来设置

       1,你可以根据每个控件占用空间的比例来指定它的权重。例如,你设置了第一个view的权重是2,第二个view的权重为1,则第一个view占据2/3的空间,第二个view占据1/3的空间。请注意设置权重之前一般先给View的宽或者高先设置为0dp,系统会根据上面设置的权重规则来计算每个控件所占的空间大小。很多情况下如果给view设置了match_parent属性,那么上面计算权重反而不按正比而是按照反比,也就是说权重越大反而所占的空间越小。

    2,在android所有的view的权重默认都是0,如果只有一个view的权重你设置了大于0,那么这个view将会占据除了其他view所占空间之外的所有空间。

    对于第一种情况

                       

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="OK" />

</LinearLayout>

得到的效果为:

                 

而第二种效果的代码为:

          

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK" />

</LinearLayout>
    得到的效果为:

               

    

    

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值