之前被这个属性困扰过好久,今天一个偶然的机会,终于把这个搞清楚了,现在与大家分享一下。

假设我们要在一个LinearLayout布局中显示两个按钮,button1和button2,button2的宽度是button1的二倍,正常情况下使用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="button1" /> <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="button2" /> </LinearLayout>

注意两个button的宽都是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" > <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button1" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="button2" /> </LinearLayout>

两个button的宽度都是match_parent,情况立马发生转变:

我以前就在这里不知道纠结了多少回,那么我们到底该怎么理解weight这个属性呢?

设置了weight属性的View的宽度等于原有宽度加上剩余空间的占比,其中剩余空间是指屏幕宽度相对于控件总宽度剩余的空间大小。

以第一种情况为例


假设屏幕宽度为L,第一个button原有宽度是0dp,那么该button的宽度为0+(L-0)×1/3 = (1/3)L

其中(L-0)中,L表示屏幕宽度,0表示两个控件的总宽度


以第二种情况为例


假设屏幕宽度为L,因为两个都是match_parent,所以总宽度是2L,那么对于第一个button而言,它的宽度为:L+(L-2L)×1/3 = (2/3)L

其中(L-2L)中,L表示屏幕宽度,2L表示两个控件的总宽度


总结:建议大家在使用weight属性的过程中把相应的宽或者高设置为0dp,这也是Google推荐的做法。水平排列的话宽设置为0dp,竖直排列的话高设置为0dp.

版权声明:本文为博主原创文章,未经博主允许不得转载。若有错误地方,还望批评指正,不胜感激。

关于weight属性使用的一些细节的更多相关文章

  1. weight属性你用的真的6嘛?

    相信大家在日常开发中一定使用过weight这个属性,它的作用一个是权重,另一个就是渲染优先级,但是你真的能很6的使用它嘛?如果不是,那么请继续往下看!!! 我们知道,当weight起到不同作用的时候, ...

  2. Android Hack1 使用weight属性实现视图的居中显示

    本文地址:http://www.cnblogs.com/wuyudong/p/5898403.html,转载请注明源地址. 如果要实现如下图所示的将按钮居中显示,并且占据父视图的一半,无论屏幕是否旋转 ...

  3. Android weight属性详解

    android:layout_weight是一个经常会用到的属性,它只在LinearLayout中生效,下面我们就来看一下: 当我们把组件宽度设置都为”match_parent”时: <Butt ...

  4. android gravity属性 和 weight属性

    来看这个布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...

  5. Android——android weight 属性(百度)

    LinearLayout 在androidUI布局中使用非常多,它其中有个很方便又很有意思的属性 weight ,这个属性理解起来不是那么简单的,而真正理解了又觉得非常简单! 下面就通过一个例子来说明 ...

  6. 【转】android gravity属性 和 weight属性

    有点忘记这两个属性了,复习一下. 来看这个布局文件 <?xml version="1.0" encoding="utf-8"?> <Linea ...

  7. Android开发技巧一--weight属性实现视图的居中(半)显示

    面试时,一位面试官问到:“如果我想讲按钮居中显示,并且占据其父视图宽度的一半,应该怎么做到呢?”即实现这种效果: 我们使用weightSum属性和layout_weight属性实现这一要求: < ...

  8. Android之使用weight属性实现控件的按比例分配空间

    从今天開始,把看书时候的知识点整理成博客, 这个比較简单,预计有经验的都用过,weight属性 在做Android布局的时候,常常遇到须要几个控件按比例分配空间的情况 比方下图效果 在底部设置两个bu ...

  9. android: android 布局中的weight 属性

    android: weight是线性布局的特有属性,控件的宽度和高度的不同,也会存在差异. 示例1:将宽度设置为包裹类型wrap_content或0dp <?xml version=" ...

随机推荐

  1. Central Europe Regional Contest 2012 Problem I: The Dragon and the Knights

    一个简单的题: 感觉像计算几何,其实并用不到什么计算几何的知识: 方法: 首先对每条边判断一下,看他们能够把平面分成多少份: 然后用边来对点划分集合,首先初始化为一个集合: 最后如果点的集合等于平面的 ...

  2. GSM、GPRS、EDGE、2G、3G与WAP的关系

    1.GSM(Global System of Mobile communication)即全球移动通讯系统: 是目前使用人数最大的移动通信网络,就是2G的移动通信技术,是一种电路交换系统.这种网络仅提 ...

  3. CISCO2691的OSPF点对点密文测评测试

    都差不多,粘一个文件就能说明问题了. Router#show run Building configuration... Current configuration : bytes ! version ...

  4. 配置.NET程序使用代理进行HTTP请求

    方式一:代码方式 var defaultProxy = new WebProxy(); defaultProxy.Address = new Uri("http://proxy:8080&q ...

  5. USB OTG

    OTG检测的原理是:USB OTG标准在完全兼容USB2.0标准的基础上,增添了电源管理(节省功耗)功能,它允许设备既可作为主机,也可作为外设操作(两用OTG).USB OTG技术可实现没有主机时设备 ...

  6. Realm for Android快速入门教程

    介绍 如果你关注安卓开发的最新趋势,你可能已经听说过Realm.Realm是一个可以替代SQLite以及ORMlibraries的轻量级数据库. 相比SQLite,Realm更快并且具有很多现代数据库 ...

  7. Eclipse can't install updates

    trying to update eclipse but after downloading updates i always get an error dialog saying: An error ...

  8. Rewriting History with Git Rebase

    http://code.tutsplus.com/tutorials/rewriting-history-with-git-rebase--cms-23191 1. Rebasing for a Li ...

  9. C#连接数据库的一些鲜为人知的方法

    用过VS2008和VS2010的开发人员肯定知道在安装这个IDE的时候会自动安装了一个精简版的SQL数据库服务SqlExpress,这个数据库系统少了最重要的企业管理器,也就是说不能用它来建数据表和一 ...

  10. BZOJ1345: [Baltic2007]序列问题Sequence

    1345: [Baltic2007]序列问题Sequence Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 556  Solved: 274[Submi ...