android weight(权重)的具体分析

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

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

对这句话不理解的可以看下图


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

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

上面算是对权重的分析,具体用法如下

先看一段代码吧

<span style="font-size:32px;"><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"
    android:weightSum="2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#66ff66"
        android:layout_weight="1"
        android:text="面码" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        android:layout_weight="1" 
        android:text="小木" />

</LinearLayout></span>

这段代码很简答,这里是水平方向为例子的。我就说下android:weightSum="2"

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

这个可以要可以不要,当你对权重不是很理解的的话建议要

上面代码的效果图



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

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

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

<LinearLayoutxmlns: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"

    android:weightSum="2">

 

    <TextView

        android:layout_width="120dp"

       android:layout_height="wrap_content"

        android:background="#66ff66"

        android:layout_weight="1"

        android:text="面码" />

 

    <TextView

       android:layout_width="30dp"

       android:layout_height="wrap_content"

        android:background="#ff0000"

        android:layout_weight="1"

        android:text="小木" />

 

</LinearLayout>

效果图


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

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

长度于是会不一样的

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

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

代码还用刚才的吧

<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"
    android:weightSum="2">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#66ff66"
         
        android:layout_weight="1"
        android:text="面码" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#ff0000"
         
        android:layout_weight="1" 
        android:text="小木" />

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

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

 以上就是我的理解,

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

那在上张图片


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

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


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

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

看下布局的代码

<LinearLayoutxmlns: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"

    android:weightSum="2">

 

    <TextView

        android:layout_width="0dp"

       android:layout_height="wrap_content"

        android:background="#66ff66"

        android:gravity="center"

        android:layout_weight="1"

        android:text="面码" />

 

    <TextView

        android:layout_width="0dp"

        android:layout_height="wrap_content"

        android:background="#ff0000"

        android:gravity="center"

        android:layout_weight="1"

        android:text="小木" />

 

</LinearLayout></span>

用到了

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

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

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







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值