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

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

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

假如我们希望剩余的空间平分给空间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 就是当前控件内容显示的位置。

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

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



android weight(权重)的详细分析的更多相关文章

  1. [Android Pro] Android签名与认证详细分析之二(CERT.RSA剖析)

    转载自: http://www.thinksaas.cn/group/topic/335449/ http://blog.csdn.net/u010571535/article/details/899 ...

  2. android关于AndroidManifest.xml详细分析

    http://my.eoe.cn/1087692/archive/5927.html 一.关于AndroidManifest.xmlAndroidManifest.xml 是每个android程序中必 ...

  3. Android Scroller类的详细分析

    尊重原创作者,转载请注明出处: http://blog.csdn.net/gemmem/article/details/7321910 Scroller这个类理解起来有一定的困难,刚开始接触Scrol ...

  4. [Android Pro] Android签名与认证详细分析之一(CERT.RSA剖析)

    转载自:http://www.thinksaas.cn/group/topic/335450/ 一.Android签名概述 我们已经知道的是:Android对每一个Apk文件都会进行签名,在Apk文件 ...

  5. Android PermissionChecker 权限全面详细分析和解决方案

    原文: http://www.2cto.com/kf/201512/455888.html http://blog.csdn.net/yangqingqo/article/details/483711 ...

  6. android异步任务asyncTask详细分析

    android中的耗时操作需要放在子线程中去执行 asynctask是对Handler和和线程池的封装,直接使用比THread效率更加的高效因为封装了线程池,比我们每次直接new Thread效率更高 ...

  7. Android中Scroller类的分析

    今天看了一下项目中用到的ViewFlow控件,想弄明白其工作原理.从头开始分析,卡在"滚动"这儿了. 做android也快两年了,连最基本的滚动都不熟悉,真是惭愧...遂网上找资料 ...

  8. Android 布局之LinearLayout 子控件weight权重的作用详析(转)

    关于Android开发中的LinearLayout子控件权重android:layout_weigh参数的作用,网上关于其用法有两种截然相反说法: 说法一:值越大,重要性越高,所占用的空间越大: 说法 ...

  9. Android 布局之LinearLayout 子控件weight权重的作用详析

    关于Android开发中的LinearLayout子控件权重android:layout_weigh参数的作用,网上关于其用法有两种截然相反说法: 说法一:值越大,重要性越高,所占用的空间越大: 说法 ...

  10. android ListView 九大重要属性详细分析、

    android ListView 九大重要属性详细分析. 1.android ListView 一些重要属性详解,兄弟朋友可以参考一下. 首先是stackFromBottom属性,这只该属性之后你做好 ...

随机推荐

  1. fieldset ----- 不常用的HTML标签

    fieldset 元素可将表单内的相关元素分组. <fieldset> 标签将表单内容的一部分打包,生成一组相关表单的字段. 当一组表单元素放到 <fieldset> 标签内时 ...

  2. Leetcode0133--Clone Graph 克隆无向图

    [转载请注明]:https://www.cnblogs.com/igoslly/p/9699791.html 一.题目 二.题目分析 给出一个无向图,其中保证每点之间均有连接,给出原图中的一个点 no ...

  3. Ubuntu安装gnome-shell桌面环境

    1.sudo apt-get install gnome-shell 输入命令直接回车就行了 2.出现了这个问题 Unable to locate package ?? sudo apt-get up ...

  4. MyEclipse中VSS的使用详解

    本文系转载,原文地址http://hi.baidu.com/yi88cheng/blog/item/13dd862f765e6b5c4fc226e5.html

  5. day05-控制流程之if/while/for

    目录 控制流程之if判断 控制流程之while循环 控制流程之for循环 控制流程之if判断 if 其实就是根据条件来做出不同的反应,如果这样就这样干,如果那样就那样干 1. 如果:成绩 > 9 ...

  6. 扩增子图表解读3热图:差异菌、OTU及功能

    热图是使用颜色来展示数值矩阵的图形.通常还会结合行.列的聚类分析,以表达实验数据多方面的结果.  热图在生物学领域应用广泛,尤其在高通量测序的结果展示中很流行,如样品-基因表达,样品-OTU相对丰度矩 ...

  7. 哈夫曼树(Huffman Tree)

    Date:2019-06-21 14:42:04 做题时更多的是用到哈夫曼树的构造思想,即按照问题规模从小到大,依次解决问题,可以得到最优解 Description: 在一个果园里,多多已经将所有的果 ...

  8. Windows下Unity安装

    安装教程: https://www.paws3d.com/lesson/us-0101/ 问题1: 安装并完成注册后,在网页上能登录,但打开Unity时不能启动成功,一直停留在如下界面 解决方案:断网 ...

  9. Luogu P3901 数列找不同

    由于技术原因,题目我贴不上了,大家点下面的链接自己去看吧^_^ P3901 数列找不同 这题第一眼看去,题面真短,有坑(flag) 在往下面看去,woc数据这么大,你要怎样. 现在一起想想想,超级侦探 ...

  10. Linux常用shell命令持续总结

    1. 查看端口运行 netstat -lnp|grep 80 2. 定时任务 Crontab -e 编辑任务 Crontab -l 查看当前任务列表 /var/log/cron-* 任务日志