对于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. 【ASC 23】G. ACdream 1429 Rectangular Polygon --DP

    题意:有很多棍子,从棍子中选出两个棍子集合,使他们的和相等,求能取得的最多棍子数. 解法:容易看出有一个多阶段决策的过程,对于每个棍子,我们有 可以不选,或是选在第一个集合,或是选在第二个集合 这三种 ...

  2. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  3. NYOJ 762

    容斥原理 http://blog.csdn.net/shiren_bod/article/details/5787722

  4. java 一些图片

  5. swift上传头像

    很久没有写博客了,今天特地写了这个,也是一边仿照别人写的demo,注释部分都是需要的.需要的同学可以参考一下. @IBAction func headImageBtnPage(){  //上传头像 / ...

  6. Linux(CentOs6.4)安装Git

    安装之前我们先来了解下git,并且要反问下:我为什么要使用git?svn用的不是很好嘛,我干嘛要换?... 问1:为什么需要版本控制系统? 版本控制是一种记录若干文件内容变化,以便将来查阅特定版本修订 ...

  7. 【BZOJ 3642】Phi的反函数

    http://www.lydsy.com/JudgeOnline/problem.php?id=3643 因为\[\varphi(n)=\prod_i p_i^{k_i-1}(p_i-1),n=\pr ...

  8. Python笔记(2)函数

    python中一切皆对象,函数也看做对象.函数被函数名所引用,但是同样的他也可以被其他标识符所引用,可以作为参数传递. def f(): return "hi" 可见a引用了函数返 ...

  9. Git从入门到学会

    Git简介 Git是什么? Git和SVN一样都是一种高效的管理代码的系统. Git是目前世界上最先进的分布式版本控制系统(没有之一). 创建版本库 什么是版本库呢?版本库又名仓库,英文名reposi ...

  10. jsp中iframe填充装个页面

    首先要引入这个css,由于我之前没有引入这个,导致iframe的高度一只是默认高度,没有改变 <style type="text/css"> body, html { ...