版权声明:本文为【欧阳鹏】原创文章,欢迎转载,转载请注明出处!

http://blog.csdn.net/ouyang_peng/article/details/50757149

关于android:layout_weight属性的详细解析

效果一

图1

上面的效果图中三个文本框的宽度比为 1:2:3

图2



代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:background="#44ff0000"
android:gravity="center"
android:text="1" /> <TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="2"
android:background="#4400ff00"
android:gravity="center"
android:text="2" /> <TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="3"
android:background="#440000ff"
android:gravity="center"
android:text="3" /> </LinearLayout>

三个TextView的layout_width都设置为0dp,layout_weight的值分别为1:2:3,所以展示的比例也为1:2:3


效果二

图3

图4

看到上面两个图片可以发现:

图3中的第一个文本框与第二、第三个文本框是不对齐的

图4中的第一个文本框与第二、第三个文本框是对齐的

图3的布局代码

下面来看图3的布局代码,如下所示

图5

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:background="#44ff0000"
android:gravity="center"
android:text="111111111111111111" /> <TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="2"
android:background="#4400ff00"
android:gravity="center"
android:text="2" /> <TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="3"
android:background="#440000ff"
android:gravity="center"
android:text="3" /> </LinearLayout>

图4的布局代码

下面来看图4的布局代码,如下所示

图6

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:background="#44ff0000"
android:gravity="center"
android:text="111111111111111111" /> <TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="2"
android:background="#4400ff00"
android:gravity="center"
android:text="2" /> <TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="3"
android:background="#440000ff"
android:gravity="center"
android:text="3" /> </LinearLayout>

可以发现图3和图4的布局文件的差别是在父布局LinearLayout中设置了android:baselineAligned=”false”


效果三

图7

图7代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_weight="1"
android:background="#44ff0000"
android:gravity="center"
android:text="111111111111111111" /> <TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="2"
android:background="#4400ff00"
android:gravity="center"
android:text="2" /> <TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="3"
android:background="#440000ff"
android:gravity="center"
android:text="3" /> </LinearLayout>

图8

图8代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_weight="1"
android:background="#44ff0000"
android:gravity="center"
android:text="111111111111111111" /> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_weight="2"
android:background="#4400ff00"
android:gravity="center"
android:text="2" /> <TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_weight="3"
android:background="#440000ff"
android:gravity="center"
android:text="3" /> </LinearLayout>

图7和图8看起来好像效果一样。

  • 图7的代码只是设置textView1的layout_width为“wrap_content”,textView2、textView3的layout_width都为“0dp”
  • 图8的代码只是设置textView1、textView2、textView3的layout_width都为“wrap_content”

效果四

图9

图9代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_weight="1"
android:background="#44ff0000"
android:gravity="center"
android:text="111111111111111111" /> <TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_weight="2"
android:background="#4400ff00"
android:gravity="center"
android:text="2" /> <TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_weight="3"
android:background="#440000ff"
android:gravity="center"
android:text="3" /> </LinearLayout>

图10

图10代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_weight="1"
android:background="#44ff0000"
android:gravity="center"
android:text="111111111111111111" /> <TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_weight="2"
android:background="#4400ff00"
android:gravity="center"
android:text="2" /> <TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_weight="2"
android:background="#440000ff"
android:gravity="center"
android:text="3" /> </LinearLayout>

图9和图10的区别是:

图9中没有展示出TextView3,而图10中展示出TextView3

而图9和图10的代码区别只是TextView3的 android:layout_weight在图9中为3,在图10中为2

下面我们来弄清楚一下到底为什么会这样:

图11

如图11所示:假设外层的父布局LinearLayout的width为480dp,

参考:布局首先按照控件声明的尺寸进行分配,然后再将剩下的尺寸按weight进行分配。

按上面的参考来计算一下三个TextView的具体width。

第一步:计算剩余的尺寸

由于三个TextView的width都设置为match_parent,即都是480dp,那么剩余的尺寸为

restWidth=480-480*3=-480*2

第二步:计算TextView1的尺寸

参考:控件的最终宽度=控件声明的宽度+父控件剩余的宽度*所占的比例

分析 如图9:

按上面的参考来计算一下三个TextView的具体width。

即:TextView1的width1如下所示:

width1=480+(-480*2)(1/6)=480(4/6)

即:TextView2的width2如下所示:

width2=480+(-480*2)(2/6)=480(2/6)

即:TextView3的width3如下所示:

width3=480+(-480*2)(3/6)=480(0/6)

所以width1:width2:width3=2:1:0 如图9所示

分析 如图10:

按上面的参考来计算一下三个TextView的具体width。

即:TextView1的width1如下所示:

width1=480+(-480*2)(1/5)=480(3/5)

即:TextView2的width2如下所示:

width2=480+(-480*2)(2/5)=480(1/5)

即:TextView3的width3如下所示:

width3=480+(-480*2)(2/5)=480(1/5)

所以width1:width2:width3=3:1:1 如图10所示

效果五

图12

有时候需要展示如图12所示的效果:子控件占据父布局宽度的1/2或者1/3等要求的时候,就需要设置父布局android:weightSum参数

图12的代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:background="#44ff0000"
android:gravity="center"
android:text="111111111111111111" />
</LinearLayout>

图12



当父布局不设置android:weightSum参数的时候如下图所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:background="#44ff0000"
android:gravity="center"
android:text="111111111111111111" />
</LinearLayout>

分析一下图12:

假设外层的父布局LinearLayout的width为480dp,

参考:布局首先按照控件声明的尺寸进行分配,然后再将剩下的尺寸按weight进行分配。

按上面的参考来计算一下三个TextView的具体width。

第一步:计算剩余的尺寸

由于三个TextView的width都设置为match_parent,即都是480dp,那么剩余的尺寸为

restWidth=480-0=480

第二步:计算TextView1的尺寸

参考:控件的最终宽度=控件声明的宽度+父控件剩余的宽度*(weight/weightSum)

TextView的width=0+480*(1/3)=160

所以TextView的宽度为父布局控件LinearLayout的1/3

图13



图13的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:background="#44ff0000"
android:gravity="center"
android:text="111111111111111111" />
</LinearLayout>

版权声明:本文为【欧阳鹏】原创文章,欢迎转载,转载请注明出处!

http://blog.csdn.net/ouyang_peng/article/details/50757149


关于android:layout_weight的更多介绍可以参考一下资源:

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!

转载请保留原文地址:http://blog.csdn.net/ouyang_peng

我的Android进阶之旅------>关于android:layout_weight属性的详细解析的更多相关文章

  1. 我的Android进阶之旅------>关于android:layout_weight属性的一个面试题

    最近碰到一个面试题,按照下图,由Button和EditText组成的界面下厨布局代码,解决这题目需要使用android:layout_weight的知识. 首先分析上图所示的界面可以看成一下3个部分. ...

  2. 我的Android进阶之旅------&gt; Android在TextView中显示图片方法

    面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包括图像的文本信息).并简要说明实现方法. 答案:Android SDK支持例如以下显示富文本信息的方式. 1.使用T ...

  3. 我的Android进阶之旅------&gt;Android字符串资源中的单引號问题error: Apostrophe not preceded by 的解决的方法

    刚刚在string字符串资源文件里,写了一个单引號.报错了,错误代码例如以下 error: Apostrophe not preceded by \ (in OuyangPeng's blog ) 资 ...

  4. 我的Android进阶之旅------&gt; Android为TextView组件中显示的文本加入背景色

    通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...

  5. 我的Android进阶之旅------&gt;Android系统设置默认来电铃声、闹钟铃声、通知铃声

    首先了解Android系统本身提供的默认铃声文件,这些文件都放在  /system/media/audio  文件夹下. /system/media/audio/ringtones   系统来电铃声 ...

  6. 【我的Android进阶之旅】Android 混淆文件资源分类整理

    之前将所有的混淆都配置在一个 proguard-rules.pro 这个Android Studio新建项目时自动生成的文件里面,而随着项目功能迭代越来越多,代码量越来越多,引用的第二方库.第三方库都 ...

  7. 我的Android进阶之旅------&gt;Android实现音乐示波器、均衡器、重低音和音场功能

    本实例来自于<疯狂Android讲义>.要实现详细的功能,须要了解下面API: MediaPlayer  媒体播放器 Visualizer 频谱 Equalizer 均衡器 BassBoo ...

  8. 我的Android进阶之旅------&gt;Android关于Activity管理的一个简单封装

    怎样管理当前的执行Activity栈,怎样彻底退出程序.本文封装了一个Activity管理类,能够方便随时退出程序. import java.util.Stack; import android.ap ...

  9. 【我的Android进阶之旅】Android使用getIdentifier()方法根据资源名来获取资源id

    有时候我们想动态的根据一个资源名获得到对应的资源id,就可以使用getResources().getIdentifier()方法来获取该id.然后再使用该id进行相关的操作. 1.Demo示例 下面用 ...

随机推荐

  1. ORACLE的显式游标与隐式游标

    1)查询返回单行记录时→隐式游标: 2)查询返回多行记录并逐行进行处理时→显式游标 显式游标例子: DECLARE CURSOR CUR_EMP IS SELECT * FROM EMP; ROW_E ...

  2. log4j容器初始化探究

    Log4j容器初始化探究 Log4j第一步就是初始化Logger容器Repository,这一章我们来探究Logger容器,从别从独立应用以及servlet容器下启动初始化两方面探究. 1 独立应用 ...

  3. PHP文件包含

    今天突然发现这个东西有好多奇怪的东西,特别写一下记一下 测试用的1.txt及phpinfo.php内容都是phpinfo() 截断: 好多.和好多./截断:这里不测试了,摘自代码审计一书,5.2可用, ...

  4. 什么是 end-to-end 神经网络?——知乎解答

    什么是 end-to-end 神经网络? https://www.zhihu.com/question/51435499 解答1 张旭 像机器一样学习,像人一样生活 YJango 等   端到端指的是 ...

  5. 【洛谷】P1040 加分二叉树

    [洛谷]P1040 加分二叉树 题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数 ...

  6. JavaScript------如何查看var变量是否是指定类型

    function isArray(a) { //Date,Array,String,Object,Function,Boolean,Number return a.constructor.toStri ...

  7. LINUX下搭建JAVA的开发环境

    LINUX下搭建JAVA的开发环境 (2009-07-13 10:04:13)     下面就将Linux下JAVA开发环境的搭建详细道来: 1.Linux下JDK的安装 至于下载JDK的二进制可执行 ...

  8. 爬虫实战【10】利用Selenium自动登陆京东签到领金币

    今天我们来讲一下如何通过python来实现自动登陆京东,以及签到领取金币. 如何自动登陆京东? 我们先来看一下京东的登陆页面,如下图所示: [插入图片,登陆页面] 登陆框就是右面这一个框框了,但是目前 ...

  9. 转义字符的理解(JAVA、字符串和正则表达式)

    一.原理总结: 要理解转义,首先要从正则表达式说起. 在正则表达式中:*和\是特殊字符:为了匹配这两个字符本身,正则表达式中需要写为\*和\\ 在Java中,只能用字符串表示正则表达式,所以需要把\* ...

  10. 巨蟒python全栈开发数据库前端6:事件onclick的两种绑定方式&&onblur和onfocus事件&&window.onload解释&&小米商城讲解

    1.回顾上节内容(JavaScript) 一.JavaScript概述 1.ECMAScript和JavaScript的关系 2.ECMAScript的历史 3.JavaScript是一门前后端都可以 ...