Android LinearLayout的android:layout_weight属性
android:layout_weight为大小权重,相当于在页面上显示的百分比,它的计算是根据LinearLayout中所有相关元素的此属性值计算的。
除了已经固定大小的,其他设置了此属性的view所占大小(长度或高度)为自己layout_weight属性值/所有layout_weight属性值*总大小。这个属性在android的sdk中都没有介绍。下面举例介绍下
比如在一个layout中显示3个TextView,第一个TextView长度占20%,第二个长度占50%,第三个占长度30%,
则比例为20%:50%:30%=2:5:3。layout代码如下
- <?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="wrap_content"
- android:layout_height="match_parent"
- android:layout_weight="2"
- android:layout_alignParentLeft="true"
- android:gravity="center_vertical"
- android:text="文本1" />
- <TextView android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:layout_weight="5"
- android:layout_alignParentLeft="true"
- android:gravity="center_vertical"
- android:text="文本2" />
- <TextView android:id="@+id/textView3"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:layout_weight="3"
- android:layout_alignParentLeft="true"
- android:gravity="center_vertical"
- android:text="文本3" />
- </LinearLayout>
从以上代码可以看出只需要设置各个TextView的android:layout_weight属性值为对应的比例即可
其中android:layout_alignParentLeft="true" android:gravity="center_vertical"是为了方便查看而设置
Android LinearLayout的android:layout_weight属性的更多相关文章
- Android:LinearLayout布局中Layout_weight的深刻理解
首先看一下LinearLayout布局中Layout_weight属性的作用:它是用来分配属于空间的一个属性,你可以设置他的权重.很多人不知道剩余空间是个什么概念,下面我先来说说剩余空间. 看下面代码 ...
- Android开发工程师文集-layout_weight讲解
前言 大家好,给大家带来Android开发工程师文集-layout_weight讲解的概述,希望你们喜欢 Layout_weight的相关代码展示 <TextView android:layou ...
- 神奇的layout_weight属性
1.概述 在线性布局有时候为了控制一下屏幕的适配,可以使用layout_weight这个属性来设置权重,要注意一点,这个属性只有在Linearlayout中才有效,这个属性往往会随着android:l ...
- Android 布局学习之——LinearLayout的layout_weight属性
一直对layout_weight属性感到比较困惑,今天学习一下,来深入了解layout_weight属性和它的用法. 定义 首先,看看Android官方文档是怎么说的,毕竟人家才是权威 ...
- Android UI: LinearLayout中layout_weight 属性的使用规则
首先来查看android sdk文档,有这么一段话 LinearLayout also supports assigning a weight to individual children with ...
- android:layout_weight属性详解 (转)
在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示.android并没用提 ...
- android:layout_weight属性详解(转)
在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示.android并没用提 ...
- android LinearLayout android:layout_weight 作用,固定比例
android 中的 LinearLayout 是线性布局有水平布局horizontal 垂直布局vertical .本文针对 水平布局horizontal 布局的weight属性做一个标记,以免 ...
- 50一个Android开发技巧(01 利用好layout_weight属性)
问题:如何将一个Button放置在布局的中间,并设置其宽度parent的50%? 分析:问题想要达到的效果应该是这样: (原文地址:http://blog.csdn.net/vector_yi/art ...
随机推荐
- 基于Packet Tracer 组建智能公司局域网
背景及要求 ...
- WebApi的缺点
post只能传递对象 必须要参数一致,不能同名称
- (算法)Partition方法求数组第k大的数
如题,下面直接贴出代码: #include <iostream> using namespace std; int Partition(int* A,int left,int right) ...
- hiberante 二级缓存设置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- Delegate模式
转载:http://www.cnblogs.com/limlee/archive/2012/06/13/2547367.html 代理模式 顾名思义就是委托别人去做事情. IOS中经常会遇到的两种情况 ...
- iptables与SELinux
iptables: 开始配置我们来配置一个filter表的防火墙.(1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables -L Chain INPUT (policy ...
- Linux c 管道文件-进程间的通信 mkfifo、pipe
管道文件: 1. 创建管道mkfifo(命名管道) #include<sys/stat.h> int mkfifo( const char *pathname, mode_ ...
- Struts2(一)
一.Struts下载地址 http://struts.apache.org/download.cgi 二.导入包.配置Web.xml和struts.xml 在下载的包中从示例中找到一些包就可以 str ...
- 算法笔记_185:历届试题 格子刷油漆(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 X国的一段古城墙的顶端可以看成 2*N个格子组成的矩形(如下图所示),现需要把这些格子刷上保护漆. 你可以从任意一个格子刷起,刷完一格,可 ...
- Element学习
生成 HTML 文档初始结构 HTML 文档的初始结构,就是包括 doctype.html.head.body 以及 meta 等内容.你只需要输入一个 “!” 就可以生成一个 HTML5 的标准文档 ...