Android布局中的layout_weight和weightSum属性的详解及使用

     由于Android设备的尺寸大小不一,种类繁多,当我们在开发应用的时候就要考虑屏幕的适配型了,尽可能让我们的应用适用于主流机型的尺寸,这样我们的应用不会因为尺寸不同而不美观,解决屏幕适配问题的方法有很多,在这里我所讲的是其中的一种解决方案---巧妙的使用layout_weight属性。

     在布局中Layout_weight属性的作用:它是用来分配属于空间的一个属性,你可以设置它所占据屏幕的权重。

1.layout_weight属性  

   首先我们先看下效果,然后再给出计算公式。

<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="horizontal" >

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

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="button2" />

</LinearLayout>

效果如下: 

然后我们修改布局文件后

<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="horizontal" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="buttton1" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="button2" />

</LinearLayout>

效果如下:


    计算公式如下:

     首先我们假设屏幕的宽度为L

     实际宽度 = 控件原来的长度 + 剩余空间所占百分比的宽度

     比如第一图片上的计算式子如下

      button1实际宽度 = 0 + 1/(1+2)L= 1/3L     button2实际宽度 = 0 +2/(1+2)L = 2/3L

     第二张图片上的计算式子如下 

      button1实际宽度 = L +1[L-(L+L)]/(1+2) L = 2/3 L    button2实际宽度 = L + 2[L-(L+L)]/(1+2) L = 1/3 L

     好了,看到这里weight属性是不是很好理解,最主要的是对剩余空间的理解 。

2.weightSum属性

android:weightSum属性在官方文档中的解释包含下面的内容:“定义weight综合的最大值,如果未指定该值,以所有子师徒的layout_weight属性的累加值作为总和的最大值。典型案例是:通过指定子视图的layout_weight属性为0.5.并设置LinearLayout的weightSum属性为1.0,实现子视图占据可用宽高的50%”。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="1" >

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="test" />

</LinearLayout>



如图,button按钮的宽度为屏幕的一半

  • 13
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值