摘要: 我的技术博客经常被流氓网站恶意爬取转载。请移步原文:http://www.cnblogs.com/hamhog/p/3907146.html,享受整齐的排版、有效的链接、正确的代码缩进、更好的阅读体验。

关于android:layout_weight到底是什么含义,在网上“越大所占比例越大”和“越大所占比例越小”两种说法都有。实际上这两种情况也都有。

例1:0dp

<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="fill_parent" android:layout_height="0dp" android:background="#ff0000" android:layout_weight="1" android:text="1"/>
<TextView android:layout_width="fill_parent" android:layout_height="0dp" android:background="#00ff00" android:layout_weight="2" android:text="2"/>
</LinearLayout>

例2:

<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#ff0000" android:layout_weight="1" android:text="1"/>
<TextView android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#00ff00" android:layout_weight="2" android:text="2"/>
</LinearLayout>

对两种情况的解释:

布局首先会给每个元素分配宽度,剩余的空间再分配给每个元素。

在分配剩余空间阶段,比例按照layout_weight,也就是越大分得越大。

在情况一里,固定宽度为0,只有第二阶段;情况二里,分配宽度能把所有空间分满,剩余的空间是负数,所以分配的越大,最终空间越小。

所以如果你要严格地让它按照某种比例,可以试试定为0dp。此时各个元素的比例即为weight。

android:layout_weight越大所占比例越大和越大所占比例越小的两个例子的更多相关文章

  1. android LinearLayout android:layout_weight 作用,固定比例

    android 中的 LinearLayout  是线性布局有水平布局horizontal  垂直布局vertical .本文针对 水平布局horizontal 布局的weight属性做一个标记,以免 ...

  2. 越大优先级越高,优先级越高被OS选中的可能性就越大

    进程的休眠:Thread sleep(1000);//括号中以毫秒为单位 当main()运行完毕,即使在结束时时间片还没有用完,CPU也放弃此时间片,继续运行其他程序. Try{Thread.slee ...

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

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

  4. android layout_weight 使用总结

    今天在使用androidlayout_weight的时候遇到点奇怪的问题,就上网查了一下,发现这篇文章很详细,就转了过来,谢谢分享者,写的很详细.  在 android开发中LinearLayout很 ...

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

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

  6. android:layout_weight详解

    参考; www.cnblogs.com/alpha-bowen/archive/2011/03/02/1969343.html 总结: 当需要对页面按比例分配时会用到这个选项: layout_weig ...

  7. android:Layout_weight的深刻理解

    最近写Demo,突然发现了Layout_weight这个属性,发现网上有很多关于这个属性的有意思的讨论,可是找了好多资料都没有找到一个能够说的清楚的,于是自己结合网上资料研究了一下,终于迎刃而解,写出 ...

  8. android layout_weight讲解

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

  9. Android的LinearLayout中的权重android:layout_weight

    当前EditText和Button部件只是适应了他们各自内容的大小,如下图所示: 这样设置对按钮来说很合适,但是对于文本框来说就不太好了,因为用户可能输入更长的文本内容.因此如果能够占满整个屏幕宽度会 ...

随机推荐

  1. Tomcat中部署WEB项目的四种方法

    对Tomcat部署web应用的方式总结,常见的有以下四种: 1.[使用控制台部署] 访问Http://localhost:8080,并通过Tomcat Manager登录,进入部署界面即可. 2.[利 ...

  2. navigationController pushViewController 多次跳转后怎么返回

    使用NavigationViewController进行页面跳转时,应该使用pushViewController方法来跳转至下一页面,这样的话,下一页面同样在NavigationViewControl ...

  3. 关于解决 The processing instruction target matching "[xX][mM][lL]" is not allowed

    在处理和保存XML文件时,出现The processing instruction target matching "[xX][mM][lL]" is not allowed 错误 ...

  4. layer.js,,,分享一个好用的弹出层

    基本属性: <!DOCTYPE html> <html> <head> <head> <meta content="text/html; ...

  5. tachyon with hadoop

    hadoop2.2.0 jdk1.7 tachyon0.5.0 无zookeeper tachyon和hadoop都是伪分布式模式 1.修改core-site.xml文件 <property&g ...

  6. Spring3 Security 中配置会话管理

    账户登录时,要求一个账户同时只能一人登录,配置中的步骤有三个: 1.在web.xml中配置HttpSessionEventPublisher <listener> <listener ...

  7. Mispelling4

    Problem Description Misspelling is an art form that students seem to excel at. Write a program that ...

  8. JavaScript组成

    JavaScript由ECMAScript,Dom,Bom三部分组成. ECMAScript是一种由Ecma国际(前身为欧洲计算机制造商协会,英文名称是European Computer Manufa ...

  9. 移动的rem自适应

    (function (doc, win) { var docEl = doc.documentElement, //html resizeEvt = 'orientationchange' in wi ...

  10. CGFloat、CGPoint、CGSize和CGRect

    CGFloat:是表示浮点数类型. CGPoint:表示二维坐标的点.通过x和y坐标定义.点表示位置值. CGSIZE:表示矩形的宽度和高度.通过宽度和高度来定义. CGRect: 表示矩形的位置和大 ...