Android layout_weight的用法
- android:layout_weight是指LinearLayout先给里面的控件分配完大小之后剩余空间的权重。
下面通过举例说明:
<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="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0045f5"
android:gravity="center"
android:text="1" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ff47"
android:gravity="center"
android:text="2"
android:layout_weight="1"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff5600"
android:gravity="center"
android:layout_weight="1"
android:text="3" /> </LinearLayout>
这个布局文件包括3个TextView控件,界面如下:

解释:由于3个文本框的宽度都是“wrap_content”,即根据视图内部内容自动扩展,LinearLayout就先给3个TextView分配适当的空间大小,
假设每个TextView分配10dp的宽度,屏幕宽度为480dp,那么LinearLayout的剩余空间就是480-3*10=450dp,由于第一个TextView没有
设置layout_weight,所以它的宽度就是10dp,而后面两个TextView设置layout_weight都是1,所以后面两个TextView就平均分配LinearLayout
的剩余空间,即为450/2=225dp,所以后面两个TextView的宽度为10+225=235dp。
- 如何让控件按比例进行空间大小的分配
将控件的width设置为0dp,然后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="horizontal" > <TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:background="#0045f5"
android:gravity="center"
android:layout_weight="1"
android:text="1" /> <TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:background="#00ff47"
android:gravity="center"
android:text="2222222222222222222"
android:layout_weight="2"/> <TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:background="#ff5600"
android:gravity="center"
android:layout_weight="3"
android:text="3" />
</LinearLayout>
效果图如下:三个控件按照1:2:3的比例进行空间分配

摘自:http://blog.csdn.net/xiaanming/article/details/13630837#
Android layout_weight的用法的更多相关文章
- 【Android学习】android:layout_weight的用法实例
对于android:layout_weight的用法,用下面的例子来说明: <LinearLayout xmlns:android="http://schemas.android.co ...
- android:layout_weight总有你不知道的用法.
都知道weight是权重的意思. 在布局中起到非常重要的作用. 但是这玩意不能嵌套使用, 而且只能使用在LinearLayout中. 下面说说它的几种用法(以下例子全为横排 注意android:lay ...
- android:layout_weight属性的使用方法总结
原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6282826.html android:layout_weight属性可以和其他属性配合使用,产生多 ...
- Android状态选择器用法总结
原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6284682.html 本文首先列出常见状态选择器的创建,然后按照常用控件来分别列出状态选择器的具体 ...
- xamarin android listview的用法
listview也许是用的非常频繁的一个控件之一,下面我写一个xamarin的listview栗子,大家尝一尝xamarin android开发的乐趣.原谅我的大小写吧. listview绑定自定义的 ...
- Android Fragment的用法(二)
如果你经常使用平板电脑,应该会发现很多的平板应用现在都采用的是双页模式(程序会在左侧的面板上显示一个包含子项的列表,在右侧的面板上显示内容),因为平板电脑的屏幕足够大,完全可以同时显示下两页的内容,但 ...
- android:layout_weight的真实含义(转)
首先声明只有在Linearlayout中,该属性才有效.之所以Android:layout_weight会引起争议,是因为在设置该属性的同时,设置android:layout_width为wrap_c ...
- android layout_weight讲解
Layout_weight是线性布局,也就是LinearLayout里面用到的,下面通过实验来看这个Layout_weight的特性. 1.当控件的属性android:layout_width=&qu ...
- Android之Adapter用法总结-(转)
Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(List View,Grid Vie ...
随机推荐
- dubbo源码分析1-reference bean创建
dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...
- 对hashmap与hashcode()、equals()的理解
1.equals方法没被重写的时候 比较的只是对象的地址 重写之后 比较的才是对象里的内容 2.重写equals的时候 务必需要重写hashcode 不然在用到容器的时候 会出现问题 因为容器会 ...
- ubuntu安装eclipse配置jdk环境
$ sudo mkdir /usr/local/java //在此目录下新建一个文件夹java $ sudo mv 下载/jdk-8u111-linux-i586.tar.gz /usr/local/ ...
- jQuery文字特效制作文字鼠标滑过多彩色变色显示
<!DOCTYPE html><head> <meta http-equiv="Content-Type" content="text/ht ...
- c# winForm使用Aspose.Cells读取CSV文件中文乱码问题
不废话直接上代码 主要注意是 红色代码部分 Aspose.Cells.TxtLoadOptions lo = new TxtLoadOptions(); lo ...
- 最有效地优化 Microsoft SQL Server 的性能
为了最有效地优化 Microsoft SQL Server 的性能,您必须明确当情况不断变化时,性能将在哪些方面得到最大程度的改进,并集中分析这些方面.否则,在这些问题上您可能花费大量的时间和精力 ...
- Hide SSH Welcome Banner/Message on Ubuntu14.04+
Introduction Usually when you logged in you linux based PC remotely via SSH, a long banner will be d ...
- LeetCode----8. String to Integer (atoi)(Java)
package myAtoi8; /* * Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- mysql 插入中文报错: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value...
总结写在前面, 总结: 当Java通过jdbc链接mysql插入中文时,要保证程序可以正常执行,而且插入的中文不会乱码, mysql服务器端,对数据表(不是数据库)的编码设置,要保证是支持中文的,例如 ...
- ubuntu环境极其内存情况
001:安装系统后 002:synergy 003:vim-cscope (修改vim脚本) 004:root 005:bashrc修改 006:bcompare 007:lib 008:git,gi ...