【Android学习】android:layout_weight的用法实例
对于android:layout_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="vertical"
tools:context="com.chenming.test.MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ff0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ff00"
android:textSize="35sp"
android:text="first"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000ff"
android:textSize="35sp"
android:text="second"/>
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ff0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ff00"
android:textSize="35sp"
android:layout_weight="1"
android:text="first"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000ff"
android:layout_weight="1"
android:textSize="35sp"
android:text="second"/>
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ff0000">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#00ff00"
android:textSize="35sp"
android:layout_weight="1"
android:text="first"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#0000ff"
android:layout_weight="1"
android:textSize="35sp"
android:text="second"/>
</LinearLayout> </LinearLayout>
以上是activity_main.xml布局文件,其效果如下:

对应刚好三行三个例子,由XML文件可以看到,红色表示空白区域,绿色表示第一个控件,蓝色表示第二个控件。
例子一:为正常的顺序排列,他们的 android:layout_width都是包含内容,所以长度为蓝色比较长,剩下的为空白区域。
例子二:在上一个例子的基础上,加入了分别加入android:layout_weight="1",weight的中文意思是权重,那么很明显,这个意思是两个控件的权重值1:1,但是要注意,这个不是说两个控件的长度是1:1,而是说在已有的长度下,把剩余的空白空间按照1:1再分给两个空间。所以,所谓的1:1指下图中的两条黑线是1:1,实际是蓝色控件长度大于绿色控件(因为second的英文长度比first长)。

例子三:那么,要如何能让蓝色控件长度等于绿色控件?只需让一开始的未分配1:1空白空间时候长度相等即可,所以例子三是在例子二的基础上把android:layout_width="wrap_content"改为android:layout_width="0dp",即宽度都为0而不是刚好包含内容,这样一来,剩余空白空间就是一整行,再按1:1分配就能是使得两个控件长度相等,也就是第三行例子三的样子。
所以android:layout_weight的用法要记住是分配剩余空白空间,而不是一开始的空间!后面改成2:3,4:5等等也都一样的道理。
【Android学习】android:layout_weight的用法实例的更多相关文章
- Android学习笔记_72_Spinner的用法
一.普通 1. <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android= ...
- android学习——android架构
android架构:在了解全局的情况下进行细致化的分析才能更有效的学习android的运行原理,才能更深刻的理解android开发: 1.架构图直观 2.架构详解 2.1.Linux Kernel 2 ...
- android学习——Android Layout标签之-viewStub,requestFocus,merge,include
定义Android Layout(XML)时,有四个比较特别的标签是非常重要的,其中有三个是与资源复用有关,分别是<viewStub/>, <requestFocus />, ...
- Android学习之Layoutinflater的用法
•她的第一次 话说,那是一个风雪交加的夜晚,看着她独自一个人走在漆黑的小道上,我抓紧跟了过去: 那晚,我们...... 记得第一次接触这个 Layoutinflater 应该是在学习 ListView ...
- android学习9——Handler简单用法
Handler用来发消息和处理消息.典型的用法是更新界面.android不允许在子线程里面更新界面,通常是把Handler传到子线程中,在子线程里通过sendEmptyMessage函数发消息.Han ...
- Android学习笔记-绘制圆形ImageView实例
现在很多的APP都很喜欢圆形的头像,这里就简单的写个圆形的ImageView~ 第三方圆形ImageView控件: RoundedImageView CircleImageView 实现代码: 自定义 ...
- Android学习----Android架构
android分为四个层,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核心层.蓝色的代表java程序,黄色的代码为运行JAVA程序而实现的虚拟机,绿色部分为C/C++语言编写 ...
- android学习——Android Studio下创建menu布局文件
一.问题: android studio项目中没有看到menu文件夹: 在android studio项目中想要添加menu布局文件,一开始我的做法是:直接在res文件夹右键选择xml文件来添加,如下 ...
- openfire Android学习---android客户端聊天开发之登录 和 注销登录
一切就绪,新建一个android测试工程: 上网权限配置,界面绘制啥的,这里就不说了. 首先 导入一个smark包.这个是用来维护长连接的,也可以是asmark.我用的是asmark 先普及一些基本知 ...
随机推荐
- HDU2767Proving Equivalences[强连通分量 缩点]
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- NOIP模拟赛20161022
NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...
- 第3章 Linux常用命令(3)_文件搜索命令
3. 文件搜索命令 3.1 文件搜索:find (1)find命令 命令名称 find 命令所在路径 /bin/find 执行权限 所有用户 语法 find [搜索范围] [-选项] [匹配条件] - ...
- Java 8简明教程
本文由 ImportNew 欢迎阅读我编写的Java 8介绍.本教程将带领你一步一步地认识这门语言的新特性.通过简单明了的代码示例,你将会学习到如何使用默认接口方法,Lambda表达式,方法引用和重复 ...
- Java并发编程:深入剖析ThreadLocal
原文出处: 海 子 想必很多朋友对ThreadLocal并不陌生,今天我们就来一起探讨下ThreadLocal的使用方法和实现原理.首先,本文先谈一下对ThreadLocal的理解,然后根据Threa ...
- 利用缓存实现APP端与服务器接口交互的Session控制
与传统B/S模式的Web系统不同,移动端APP与服务器之间的接口交互一般是C/S模式,这种情况下如果涉及到用户登录的话,就不能像Web系统那样依赖于Web容器来管理Session了,因为APP每发一次 ...
- ReactJS基础视频教程
React是什么?React.js 是 Facebook 推出的一个用来构建用户界面的 JavaScript 库.Facebook开源了React,这是该公司用于构建反应式图形界面的JavaScrip ...
- Js前端代码异常监控
window.onerror = function(msg,url,line,col,error){ //没有URL不上报!上报也不知道错误 if (msg != "Script error ...
- MVP之V和P的交互
三者之间的关系 在MVP初探里简单的描述了V和P之间是如何交互的. 无论是PV还是SC,M\V\P这三者之间的关系并没有发生改变,V只是前端的客户代理承现展显数据,P是如何处理客户交互行为的决策者. ...
- Apache Shiro系列三,概述 —— 10分钟入门
一.介绍 看完这个10分钟入门之后,你就知道如何在你的应用程序中引入和使用Shiro.以后你再在自己的应用程序中使用Shiro,也应该可以在10分钟内搞定. 二.概述 关于Shiro的废话就不多说了 ...