<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】两个按钮的宽度各占屏幕的一半
  2. Android新增AppCompatTextView自适应字体大小和文本宽度
  3. C++中动态内存分配与命名空间介绍
  4. c语言怎么实现动态内存分配
  5. c语言数组在内存中是怎么分配的?
  6. 简述分配器的作用是什么?
  7. Kafka 原理以及分区分配策略剖析
  8. 操作系统分配存储时网络无法正常连接
  9. Python 内存分配时的小秘密

随机推荐

  1. 安卓NDK的开发
  2. Android(安卓)RelativeLayout的一些常用
  3. Android(安卓)ListView列表 刷新和加载更
  4. 多点触控测试代码 PointerLocation
  5. Android(安卓)Wear - Creative Vision fo
  6. listView加快scroll
  7. Android(安卓)ImageButton android:scale
  8. android 圆环(ring)drawable 画法 ——简单
  9. Android中有关布局的几个问题
  10. Android: WebView tips & tricks