当我们使用linearlayout线性布局,放置三个textview空间,设置android:layout_width属性为wrap_content,并分别设置android:layout_weight比重为1,2,3时:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:layout_width="fill_parent"
3 android:layout_height="fill_parent" >
4
5 <TextView
6 android:id="@+id/textView1"
7 android:layout_width="wrap_content"
8 android:layout_height="wrap_content"
9 android:layout_weight="1"
10 android:background="#990033"
11 android:text="1" />
12
13 <TextView
14 android:id="@+id/textView2"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:layout_weight="2"
18 android:background="#339933"
19 android:text="2" />
20
21 <TextView
22 android:id="@+id/textView3"
23 android:layout_width="wrap_content"
24 android:layout_height="wrap_content"
25 android:layout_weight="3"
26 android:background="#0000CC"
27 android:text="3" />
28
29 </LinearLayout>

可以看到三个textview所占的宽度比为1:2:3,似乎时根据我们所设置的android:layout_width比重实现了比例控制,然而,当我们试着把textview1的文本内容修改为111111111时

原有的比例平衡被打破了,现在三个textview的宽度比不再是1:2:3。为什么呢?因为layout_width属性并不对整个可用空间进行分配,而是对剩余空间进行分配,也就是说系统会先按layout_width设置的属性先给控件分配空间,在这里的代码里是先分配空间使得每个控件足以包裹住内容,再将剩余的内容按layout_weight所设置的比例进行分配,控件最终的空间大小是layout_width属性设置的空间大小加上按比例分得的剩余空间大小,你细心观察将可以发现,上图UI中除去内容之外的控件还是1:2:3。

  这时候我们layout_width的属性改为0dp试试:

这时出现了两行 可使用 android:maxLine="1"

我们再来看一个更好玩的,将layout_width设置为match_parent属性,再将三个控件比例设置为1:2:2:

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:layout_width="fill_parent"
3 android:layout_height="fill_parent" >
4
5 <TextView
6 android:id="@+id/textView1"
7 android:layout_width="match_parent"
8 android:layout_height="wrap_content"
9 android:layout_weight="1"
10 android:background="#990033"
11 android:text="1" />
12
13 <TextView
14 android:id="@+id/textView2"
15 android:layout_width="match_parent"
16 android:layout_height="wrap_content"
17 android:layout_weight="2"
18 android:background="#339933"
19 android:text="2" />
20
21 <TextView
22 android:id="@+id/textView3"
23 android:layout_width="match_parent"
24 android:layout_height="wrap_content"
25 android:layout_weight="2"
26 android:background="#0000CC"
27 android:text="2" />
28
29 </LinearLayout>

不是说好按比例控制的吗?为什么textview1所占的比例为1,确比占比为2的控件获得的空间更大的呢?

  我们来用刚刚的公式好好算算:

  剩余空间,是用父控件的大小也就是1个parent减去layout_width所设置的属性大小,每个控件原本的大小都设置为match_parent,也就是每个控件原本就应该获得1个parent控件的大小,这里有3个textview控件,也就是剩余空间的大小=1个parent-3个parent大小=-2个parent,于是textview1的大小=原来的大小1个parent+分配的剩余空间大小就是(-2parent)*1/5,最后等于3/5个parent,而其余的两个控件可以算出是1/5个parent,所以比例是3:1:1。

android设置组件所占的比例的更多相关文章

  1. android设置组件透明度

    textremind.setBackgroundColor(Color.argb(178, 0, 0, 0));  //背景透明度 textremind.setTextColor(Color.argb ...

  2. android动态设置组件LayoutParams

    开发中经常用到动态设置组件的LayoutParams,之前开发遇到的问题如下: LinearLayout.LayoutParams params = new LinearLayout.LayoutPa ...

  3. 【ALearning】第四章 Android Layout组件布局(一)

    在本章中,我们将Android学习组件布局.在前面的章节,我们也开始使用LinearLayout布局.然后我们在布局文件更加具体的学习和理解,会. Android的界面是有布局和组件协同完毕的,布局好 ...

  4. Android 四大组件之 Activity(二)

    1.综述 Activity是Android四大组件(Application Components)之一,简单来说Activity就是平常所见到的用户界面,一般情况下,一个Activity所占的窗口是满 ...

  5. Android设置头像,手机拍照或从本地相冊选取图片作为头像

     [Android设置头像,手机拍照或从本地相冊选取图片作为头像] 像微信.QQ.微博等社交类的APP,通常都有设置头像的功能,设置头像通常有两种方式: 1,让用户通过选择本地相冊之类的图片库中已 ...

  6. Android自定义组件系列【4】——自定义ViewGroup实现双侧滑动

    在上一篇文章<Android自定义组件系列[3]--自定义ViewGroup实现侧滑>中实现了仿Facebook和人人网的侧滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布局示 ...

  7. Android——四大组件、六大布局、五大存储

    一.android四大组件 (一)android四大组件详解 Android四大组件分别为activity.service.content provider.broadcast receiver. 1 ...

  8. Android业务组件化之URL Scheme使用

    前言: 最近公司业务发展迅速,单一的项目工程不再适合公司发展需要,所以开始推进公司APP业务组件化,很荣幸自己能够牵头做这件事,经过研究实现组件化的通信方案通过URL Scheme,所以想着现在还是在 ...

  9. android四大组件之Broadcast

    广播的概念 现实中:我们常常使用电台通过发送广播发布消息,买个收音机,就能收听 Android:系统在产生某个事件时发送广播,应用程序使用广播接收者接收这个广播,就知道系统产生了什么事件.Androi ...

随机推荐

  1. cocos2d-x 2.2.3 之菜单分析(1)

    TextEdit-Menu CCtextFieldTTF cocos2d – x 中提供的 bool T04ZORDER::init() { if (!CCLayer::init()) { retur ...

  2. AngularJS clone directive 指令复制

    需求背景:         directive模块化某表单信息,但表单信息可加入多条.此时就涉及到clone directive. 解决方式:         能够通过使用angularjs中$com ...

  3. [转] Console命令详解,让调试js代码变得更简单

    http://www.cnblogs.com/see7di/archive/2011/11/21/2257442.html Firebug是网页开发的利器,能够极大地提升工作效率. 但是,它不太容易上 ...

  4. 如何完全退出android应用程序

    当一个android应用程序包含多个activity时,要完全退出android应用程序,便要销毁掉所有的activity,下面是一种网上流传的比较经典完美的方法: 首先要定义一个继承Applicat ...

  5. 数据库 SQL 外键约束 多表查询

    多表设计与多表查询 1.外键约束        表是用来保存现实生活中的数据的,而现实生活中数据和数据之间往往具有一定的关系,我们在使用表来存储数据时,可以明确的声明表和表之前的依赖关系,命令数据库来 ...

  6. json数组传递到后台controller

    现前台有如下格式的数据需要传递到后台的controller, public class UpdatePara { public int RoleID { get; set; } public List ...

  7. IIS应用程序池数目

    今天突然遇到一个问题,访问线上站点,数据处理过之后,访问页面有时是404,有时不是404,而404页面刷新几次之后就正常了. 经过大神们指点之后,发现竟然是应用程序池惹的祸. 分析下原因,在代码里面数 ...

  8. 怎样取得数组对象和arralist 的长度

    数组用length属性 ArrayList用size()方法

  9. iptables 下开放ftp

    这两天在给客户安装服务器时也顺便给他们使用iptables,不用不知道,一用才发现iptables还有很多东西可以学的,比如开放ftp.iptables 的filter表的INPUT链的默认策略设为了 ...

  10. Tomcat:Can't load IA 32-bit .dll on a AMD 64-bit platform问题的解决

    控制台错误如下: java.lang.UnsatisfiedLinkError: D:\apache-tomcat-7.0.56\bin\tcnative-1.dll: Can't load IA 3 ...