<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="match_parent"          android:layout_height="40dp"        android:layout_weight="1"/>        <TextView          android:layout_width="match_parent"          android:layout_height="40dp"        android:layout_weight="2"/>        <TextView          android:layout_width="match_parent"          android:layout_height="40dp"        android:layout_weight="2"/>  </LinearLayout>  

这里为每个TextView设置的宽度为”match_parent",即为充满整个LinearLayout,假如屏幕依然为320dip, 首先LinearLayout为3个TextView分配的宽度为320dip,屏幕剩余宽度为 320 - 3* 320 = -640dip,然后3个TextView按照权重分配剩余空间,第一个TextView分配宽度为 320 + (-640) * (1/5) = 192dip,后面两个TextView就为320 + (-640) * (2/5) = 64dip,则三个TextView权重比例实际上为3:1:1,而不是1:2:2。

谷歌官方推荐如果设置layout_weight属性的时候,最好将layout_width设置为“0dp",这样才能按照1:2:2的比例为三个TextView分配空间。

stackoverflow.com上对剩余空间计算的解释。

http://stackoverflow.com/questions/7452741/what-is-androidweightsum-in-android-and-how-does-it-work

更多相关文章

  1. android LinearLayout 相关知识
  2. Android帧布局-实现渐变效果
  3. TextView的android:maxHeight,android:minHeight的正确设置
  4. EditText的一些使用注意点
  5. [Android] 零碎知识汇总 - Shape相关
  6. GridView控件的简单使用
  7. Android(安卓)Studio 布局属性笔记
  8. Android中android:layout_width与 android:width有什么区别?
  9. 【android】两个按钮的宽度各占屏幕的一半

随机推荐

  1. PHP-01原理
  2. 安装ApiPost-接口工具 发送HTTP请求
  3. 命名空间的相关知识
  4. 一个前端后端的综合实例
  5. mysql的基础知识
  6. 总结mysql 常用DDL, DML语言并实操;
  7. 缓存函数的简单使用
  8. Windows还原系统后分区合并为一个分区恢
  9. php简单的数组递归
  10. 通过链式调用,将一个类委托给另一个类实现