摘要: 我的技术博客经常被流氓网站恶意爬取转载。请移步原文: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。

更多相关文章

  1. android通过web service调用查询手机归属地代码
  2. Android屏幕100%适配方案
  3. android获取手机内部存储空间和外部存储空间
  4. android 连接 wsdl webservice 解析
  5. Android(安卓)studio: Gradle sync failed
  6. Error parsing XML: unbound prefix on XXX解决办法
  7. android Audio设置音量流程及其binder通讯原理
  8. android 图片浏览 处理
  9. Android单个进程内存分配

随机推荐

  1. Android string.xml文件中使用格式化符号
  2. android工程下不能运行java main程序的解
  3. Android签名机制学习笔记
  4. 【Android RTMP】安卓直播推流总结 ( 直
  5. android核心基础(12)_创建模拟器时常见问
  6. Android:unable to start Activity ...的
  7. android chess---main.xml
  8. Android:FragmentTransaction
  9. android 显示gif格式的图片
  10. 1.2RelativeLayout