当我们使用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. struts2在web.xml中配置详情

    web.xml是web应用中载入有关servlet信息的重要配置文件,起着初始化servlet,filter等web程序的作用. 通常,全部的MVC框架都须要Web应用载入一个核心控制器.那採取什么方 ...

  2. 具体解释VB中连接access数据库的几种方法

    在VB中,连接ACCESS数据库的方法主要有以下三种 使用ADO对象,通过编写代码訪问数据库 Connection 对象 ODBC数据源 使用ADO Data 控件高速创建数据库连接 有三种连接方法 ...

  3. [CSS3 + HTML5] Modernizr

    Modernizr is a library for detecting whether the user's browsers have certain features and based on ...

  4. Android 自定义View (三) 圆环交替 等待效果

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24500107 一个朋友今天有这么个需求(下图),我觉得那自定义View来做还是很 ...

  5. $.each 和$(selector).each()的差别

    Home » jQuery » $.each() $.each() Posted on 2012 年 3 月 15 日 in jQuery, jQuery函数 | by Jason | 译自官方手冊: ...

  6. yii cgridview 如何显示图片

    发布的新闻或介绍里有图片,图片存的是Url地址,通过yii的cgridview,默认是数据库里存的啥就显示啥,如何把url地址转为图片?直接上代码 <?php $data = $model-&g ...

  7. POJ2914

    POJ2914 无向图的最小割 题意:给你一个无向图,然后去掉其中的n条边,使之形成两个连通分量,也即原无向图不连通,求n的最小值. 输入: m(无向图点集),n(无向图边集) a,b,c(a,b两点 ...

  8. 再谈Cookies欺骗

    在上一篇关于cookies欺骗的随笔中,提到的解决方案是把密码MD5加密之后存入cookies中,确实这种方法实现了效果,不过把密码留在客户端等待着去被破解不是一个合适的方法,在此也感谢 @老牛吃肉 ...

  9. Asp.Net WebApi 启用CORS跨域访问指定多个域名

    1.后台action指定 EnableCors指定可访问的域名多个,使用逗号隔开 //支持客户端凭据提交,指定多个域名,使用逗号隔开 [EnableCors("http://localhos ...

  10. iOS 网络与多线程--7.Performselector消息处理方法

    创建一个IOSApp类 IOSApp.h文件 #import <Foundation/Foundation.h> @interface IOSApp : NSObject // 1.添加一 ...