• 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的用法的更多相关文章

  1. 【Android学习】android:layout_weight的用法实例

    对于android:layout_weight的用法,用下面的例子来说明: <LinearLayout xmlns:android="http://schemas.android.co ...

  2. android:layout_weight总有你不知道的用法.

    都知道weight是权重的意思. 在布局中起到非常重要的作用. 但是这玩意不能嵌套使用, 而且只能使用在LinearLayout中. 下面说说它的几种用法(以下例子全为横排 注意android:lay ...

  3. android:layout_weight属性的使用方法总结

    原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6282826.html android:layout_weight属性可以和其他属性配合使用,产生多 ...

  4. Android状态选择器用法总结

    原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6284682.html 本文首先列出常见状态选择器的创建,然后按照常用控件来分别列出状态选择器的具体 ...

  5. xamarin android listview的用法

    listview也许是用的非常频繁的一个控件之一,下面我写一个xamarin的listview栗子,大家尝一尝xamarin android开发的乐趣.原谅我的大小写吧. listview绑定自定义的 ...

  6. Android Fragment的用法(二)

    如果你经常使用平板电脑,应该会发现很多的平板应用现在都采用的是双页模式(程序会在左侧的面板上显示一个包含子项的列表,在右侧的面板上显示内容),因为平板电脑的屏幕足够大,完全可以同时显示下两页的内容,但 ...

  7. android:layout_weight的真实含义(转)

    首先声明只有在Linearlayout中,该属性才有效.之所以Android:layout_weight会引起争议,是因为在设置该属性的同时,设置android:layout_width为wrap_c ...

  8. android layout_weight讲解

    Layout_weight是线性布局,也就是LinearLayout里面用到的,下面通过实验来看这个Layout_weight的特性. 1.当控件的属性android:layout_width=&qu ...

  9. Android之Adapter用法总结-(转)

    Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(List View,Grid Vie ...

随机推荐

  1. jquery判断页面是否滑动到最底部

    // 滚动到底部,向下的箭头消失 var $down = $('.down'); var $window = $(window); var $document = $(document); $wind ...

  2. DuiLib学习笔记5——标题栏不能正常隐藏问题

    我之前代码都是照着官方那个Duilib入门文档.doc来学习的.但是遇到一个问题,虽然他隐藏了windows的自带标题栏,可以自己绘画一个标题栏了,但是在这个标题栏下方,用力乱戳,就可能把系统自带的, ...

  3. RAC One Node转换为RAC

    1.查看状态 [oracle@rone1 ~]$ srvctl config database -d rone Database unique name: rone Database name: ro ...

  4. 20145320 《Java程序设计》第8周学习总结

    20145320 <Java程序设计>第8周学习总结 教材学习内容总结 15.1日志 java.util.logging包提供了日志功能相关类与接口,不必额外配置日志组件,就可以在标准ja ...

  5. zjuoj 3608 Signal Detection

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3608 Signal Detection Time Limit: 2 Sec ...

  6. VS2010 支持 CSS3

    在安装Standards Update for VS2010 SP1后,VS2010中没有CSS3.0问题,以下是我的解决方法 1.首先去官网下载 CSS 3 Intellisense Schema ...

  7. 关于SSIS中解密数据库字符串的方法

    此文章适合于SSIS新手,我是个小白,在繁复查阅资料后仍无果到最后解决问题,走了很多弯路,现在讲其中一些关于SSIS的理解写出来,供大家参考,在正文之前,我就我自己的理解,阐明一些概念. 什么是SSI ...

  8. mysql常用命令(1)

    Mysql安装目录数据库目录/var/lib/mysql/配置文件/usr/share/mysql(mysql.server命令及配置文件)相关命令/usr/bin(mysqladmin mysqld ...

  9. 关于</div>的粗浅理解

    </div>作为c#中常用的一个标签,在写多个区域的内容时有着十分重要的作用.如果写简单的网页时不用div可能感受不到太大的影响,但是在写较为复杂的程序时div的分隔作用就很明显了,改动大 ...

  10. easyui filter 过滤时间段

    $.extend($.fn.datagrid.defaults.filters, { dateRange: { init: function(container, options){ var c = ...