android: weight是线性布局的特有属性,控件的宽度和高度的不同,也会存在差异。

示例1:将宽度设置为包裹类型wrap_content或0dp

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".WeightApiUseDemoActivity"
android:orientation="horizontal"
> <Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"
/> <Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button 2"
/> </LinearLayout>

运行:

示例2: 将宽度设置为match_parent时

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".WeightApiUseDemoActivity"
android:orientation="horizontal"
> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"
/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button 2"
/> </LinearLayout>

运行:

小结: 第一种现象很好理解,当放置两个宽度为0dip或是wrap_content的按钮时,由于宽度并未确定,那Button1所占的宽度就是 1 / (1+2) = 1/ 3, 也就是总长度的1/3;Button2 所占的宽度是 2 / (1+2) = 2 / 3, 也就是总长度的2/3。

第二种现象就很奇怪了,其实这是因为View在绘制的时候有一个规则,如果给View添加了android:weight属性,那么这个View的最终宽度 = View本来定义的宽度 +   View在LinearLayout中剩余空间中所占比例的宽度。什么意思呢,假设线性布局的总宽度为 L ,

拿第一种现象来说

Button1的宽度 = 0dip + ( L - Button1的宽度(也就是0dip) - Button2的宽度(也是0dip) ) * 1/3 = 0dip + L * 1/3 = 1/3 L。
Button2的宽度 =  0dip + ( L - Button1的宽度(也就是0dip) - Button2的宽度(也是0dip) ) * 2/3 = 0dip + L * 2/3 = 2/3 L。

拿第二种现象来说

Button1的宽度 = L + ( L - Button1的宽度(也就是L) - Button2的宽度(也是L) ) * 1/3 = L + (-L) * 1/3 = L - 1/3 L = 2/ 3L。
Button2的宽度 = L + ( L - Button1的宽度(也就是L) - Button2的宽度(也是L) ) * 2/3 = L + (-L) * 2/3 = L - 2/3 L = 1/ 3L。

android: android 布局中的weight 属性的更多相关文章

  1. 这些Android系统样式中的颜色属性你知道吗?

    Android 系统样式中的颜色属性 推荐阅读看完后彻底搞清楚Android中的 Attr . Style .Theme 几个常用的颜色属性 先放上一张经典的图片,图片来自网络. 这张图在网上很是流传 ...

  2. 从零开始学android开发-布局中 layout_gravity、gravity、orientation、layout_weight

    线性布局中,有 4 个及其重要的参数,直接决定元素的布局和位置,这四个参数是 android:layout_gravity ( 是本元素相对于父元素的重力方向 ) android:gravity (是 ...

  3. android 在布局中动态添加控件

    第一步 final LayoutInflater inflater = LayoutInflater.from(this); 第二步:获取需要被添加控件的布局 final LinearLayout l ...

  4. android 相对布局里面的一些属性

    一.   有关于RelativeLayout布局的一些属性 1.  相对于兄弟控件的位置:android:layout_below Android:layout_toLeftof Android:la ...

  5. [转] android自定义布局中的平滑移动

    无意中搜索到这篇文章,大概扫了一眼,知道是篇好文,先转载记录下来学习! 文章主要讲的是自定义view的写法心得. 转自:http://www.apkbus.com/android-48445-1-1. ...

  6. AndroidのUI布局之layout weight

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  7. flex布局中父容器属性部分演示效果

    如图可见flex的属性分为父容器和子容器的属性共12个.关于这些属性具体代表什么意思,网上有很多教程的文章,自觉不能写得比别人更好,所以这里主要写了一些例子关于父容器属性效果的演示,希望可以帮助大家理 ...

  8. android 基本布局(RelativeLayout、TableLayout等)使用方法及各种属性

        本文介绍 Android 界面开发中最基本的四种布局LinearLayout.RelativeLayout.FrameLayout.TableLayout 的使用方法及这四种布局中常用的属性. ...

  9. android布局中显示隐藏动画

    android 在布局中提供属性,能简单的加入动画效果,例如以下: <LinearLayout ... animateLayoutChanges="true" ... /&g ...

随机推荐

  1. C# 里面将字符作为代码计算,主要是运算符号的计算

    DataTable dt = new DataTable(); string str="1+2*(5+3)+3-1"; dt.Compute(str, "false&qu ...

  2. python的map和reduce函数

    map函数时python的高级内置函数 语法为:map(function, iterable, ...) 参数:function -- 函数iterable -- 一个或多个序列 将function作 ...

  3. Mysql中innodb和myisam

    innodb和myisam两种存储引擎的区别 1.事务和外键 1)InnoDB具有事务,支持4个事务隔离级别,回滚,崩溃修复能力和多版本并发的事务安全,包括ACID.如果应用中需要执行大量的INSER ...

  4. python使用print写文件

    刚刚看sumo的官方教程,有一段代码是生成配置文件,发现python中的print函数也可以写文件. with open("data/cross.rou.xml", "w ...

  5. 微信小程序中padding-right和margin-right无效

    在小程序中遇到样式padding-right和margin-right无效,调试发现设置了padding后,宽度已经大于页面的实际宽度,除了设置float:right之外,找不到办法让右侧paddin ...

  6. 字节序 —— Big Endian 和 Little Endian

    一.字节序 字节序指的是多字节的数据在内存中的存放顺序 内存有高地址端与低地址端.其中,低地址端既可以存放高位字节,也可以存放低位字节. Big Endian 是指低地址端 存放 高位字节. Litt ...

  7. AndroidStudio中Flutter打包APK

    1.生成签名文件 在打包之前我们需要一个签名文件,证明文件的唯一性. keytool -genkey -v -keystore F:\APP\sign.jks -keyalg RSA -keysize ...

  8. LeetCode 959. Regions Cut By Slashes

    原题链接在这里:https://leetcode.com/problems/regions-cut-by-slashes/ 题目: In a N x N grid composed of 1 x 1 ...

  9. JSP九大隐式对象和四大域对象-----面试

    因为jsp实质是一个Servlet对象:jsp在第一次访问时会被Web容器翻译成Servlet,在执行过程:第一次访问---->inex.jsp---->index_jsp.java--- ...

  10. ipcm

    用来删除一个或更多的消息队ipcm列.信号量集或者共享内存标识