android 高度上分权重,android weight(权重)的详细分析

首先要明确权重分配的是那些空间?

权重是依照比例分配屏幕的剩余空间

对这句话不理解的能够看下图

47c82c84bb9ef4967c75436497122736.png

假如我们希望剩余的空间平分给空间1 和空间2 ,

我们分别在2个控件的设置android:layout_weight="1"

上面算是对权重的分析,详细使用方法例如以下

先看一段代码吧

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

android:weightSum="2">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#66ff66"

android:layout_weight="1"

android:text="面码" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#ff0000"

android:layout_weight="1"

android:text="小木" />

这段代码非常简答。这里是水平方向为样例的。

我就说下android:weightSum="2"

这个是权重分的总个数。这里我分为2分,

这个能够要能够不要。当你对权重不是非常理解的的话建议要

上面代码的效果图

0bd7151c2df29ee8426b8beab558ac37.png

我把背景颜色设置不同,方便大家看呢,这时候两者是平分的,

原因是控件的初始长度一样,都是wrap_content,为了便于区分

权重分配的是剩余的空间。把初始长度设置为不一样,看以下代码

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

android:weightSum="2">

android:layout_width="120dp"

android:layout_height="wrap_content"

android:background="#66ff66"

android:layout_weight="1"

android:text="面码" />

android:layout_width="30dp"

android:layout_height="wrap_content"

android:background="#ff0000"

android:layout_weight="1"

android:text="小木" />

效果图

a1420e84e33d1d8b9e5e27553e1f3c72.png

非常明显不一样了,原因也就是两者控件初始化长度

不一样,把剩余的空间平分给他们之后他们的

长度于是会不一样的

以上就是整个项目布局完之后我对权重的理解,

对了提一下,项目中我一般设置 android:layout_width="0dp"

代码还用刚才的吧

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

android:weightSum="2">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:background="#66ff66"

android:layout_weight="1"

android:text="面码" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:background="#ff0000"

android:layout_weight="1"

android:text="小木" />

为啥要把长度设置为0呢?这就要从我写的第一句话看是想了

那就为了为了能更好的分配剩余的空间。忽略掉初始的长度。

以上就是我的理解,

补充下http://blog.csdn.net/qq_33210042/article/details/50902052

那在上张图片

ceb130c66df12124e69e578016e56653.png

开发中会常常遇到把字放到控件的中间在用viewgroup滑动同一时候

能改变字体的颜色。详细的实现就不说了,这里说下布局

95aba4926493878b80500c282a49e2ff.png

我们要的就是这种把。左右滑动点击同一时候也能切换,

当然有时候不止2个那就把权重多分几份。

看下布局的代码

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

android:weightSum="2">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:background="#66ff66"

android:gravity="center"

android:layout_weight="1"

android:text="面码" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:background="#ff0000"

android:gravity="center"

android:layout_weight="1"

android:text="小木" />

用到了

android:gravity 就是当前控件内容显示的位置。

这里我是顺便提下,假设换有对不布局不理的童鞋

关注我的博客。我会个大家一同进步的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值