对于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的用法实例的更多相关文章

  1. Android学习笔记_72_Spinner的用法

    一.普通 1. <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android= ...

  2. android学习——android架构

    android架构:在了解全局的情况下进行细致化的分析才能更有效的学习android的运行原理,才能更深刻的理解android开发: 1.架构图直观 2.架构详解 2.1.Linux Kernel 2 ...

  3. android学习——Android Layout标签之-viewStub,requestFocus,merge,include

    定义Android Layout(XML)时,有四个比较特别的标签是非常重要的,其中有三个是与资源复用有关,分别是<viewStub/>, <requestFocus />, ...

  4. Android学习之Layoutinflater的用法

    •她的第一次 话说,那是一个风雪交加的夜晚,看着她独自一个人走在漆黑的小道上,我抓紧跟了过去: 那晚,我们...... 记得第一次接触这个 Layoutinflater 应该是在学习 ListView ...

  5. android学习9——Handler简单用法

    Handler用来发消息和处理消息.典型的用法是更新界面.android不允许在子线程里面更新界面,通常是把Handler传到子线程中,在子线程里通过sendEmptyMessage函数发消息.Han ...

  6. Android学习笔记-绘制圆形ImageView实例

    现在很多的APP都很喜欢圆形的头像,这里就简单的写个圆形的ImageView~ 第三方圆形ImageView控件: RoundedImageView CircleImageView 实现代码: 自定义 ...

  7. Android学习----Android架构

    android分为四个层,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核心层.蓝色的代表java程序,黄色的代码为运行JAVA程序而实现的虚拟机,绿色部分为C/C++语言编写 ...

  8. android学习——Android Studio下创建menu布局文件

    一.问题: android studio项目中没有看到menu文件夹: 在android studio项目中想要添加menu布局文件,一开始我的做法是:直接在res文件夹右键选择xml文件来添加,如下 ...

  9. openfire Android学习---android客户端聊天开发之登录 和 注销登录

    一切就绪,新建一个android测试工程: 上网权限配置,界面绘制啥的,这里就不说了. 首先 导入一个smark包.这个是用来维护长连接的,也可以是asmark.我用的是asmark 先普及一些基本知 ...

随机推荐

  1. 第一个JAVA应用

    1.1创建源文件 1.1.1Java源文件结构 Java应用由一个或多个扩展名为“.Java”的文件构成,这些文件被成为源文件.从编译角度,则被称为编译单元(Compilation Unit). 如果 ...

  2. Struts2 Ajax校验

    Ajax(Asynchronous javascript and xml):异步刷新技术 技术组成:  CSS + xml +JavaScript +DOM Ajax核心对象: XMLHttpRequ ...

  3. flex align-content中的描述的“多根轴线的对齐方式”中的“多根轴线”到底是什么

    flex 有两条轴线,根据flex-flow 设置的来判断的,水平为主轴的话,那么值为row,垂直为主轴的话那么为column: 其中设置align-items 和 align-content都是来设 ...

  4. 搭建一套自己实用的.net架构(4)【CodeBuilder-RazorEngine】

    工欲善其事必先利其器,  下面来说说代码生成器. 现在代码生成器品种繁多各式各样, 什么codesmith.T4. 动软也算.那么每款代码生成器都有自己模板解析引擎. 现在比较流行的 NVelocit ...

  5. LCS问题

    最长公共子序列问题 1.这里pre数组表示回溯的方式,状态转移方程!!! #include <iostream> #include <algorithm> #include & ...

  6. vue 配置文件详解

    var path = require('path'); // NodeJS中的Path对象,用于处理目录的对象,提高开发效率. // 模块导入 module.exports = { // 入口文件地址 ...

  7. 用JS写了一个打字游戏,反正我是通不了关

    今天想写个简单的游戏, 打字游戏好像都没写过, 那么就写打字游戏吧, gamePad包含了关卡的信息, 可以用来调整给个关卡字符下落的速度: getRandom函数会返回一个字符对象, 这个对象包含了 ...

  8. VS 团队资源管理 强制解锁锁定文件

    故事是这样发生的: 以前有台电脑,在团队资源里看程序,可能冥冥中不小心按了个空格,so,文件被锁定 而我却没有发现 如果再给我一个机会,我只想说记得签入 然后,高潮来了 重装电脑 欣喜的装好新机子打开 ...

  9. python标准模块(三)

    本文会涉及到的模块: subprocess logging 1. subprocess 可以执行shell命令的相关模块和函数有: os.system os.spawn os.popen --废弃 p ...

  10. 面试web前端开发,被打击了

    今天我去面试web前端开发,被打击了,也跟我也有一定的关系,最基础的东西我都没回答好,哎! 主要是我在等我有意向的公司给我发offer,闲着没事,刚好又有公司叫我去面试,我抱着多面一家也没有啥子坏处就 ...