首先来查看android sdk文档,有这么一段话

LinearLayoutalso supports assigning aweightto individual children with theandroid:layout_weightattribute. This attribute assigns an "importance" value to a view in terms of how much space is should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.

我们从文档中可以看出LinearLayout中子控件可以使用layout_weight 属性来分配所占的“权重”,但是实际使用的过程中,经常会出错,会让人感到疑惑,下面是自己总结的一些经验:

以水平(垂直方向类似)LinearLayout为例:

<LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginLeft="16dp"    android:layout_marginRight="16dp" ><Button    android:id="@+id/crime_date"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_weight="1" /><CheckBox    android:id="@+id/crime_solved"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_weight="1"    android:text="@string/crime_solved_label" /></LinearLayout>

LinearLayout是个水平线性布局,会根据每个子成员layout_width和layout_weight来决定应该给他们分配多少空间, 遵循以下规则:

  1. 首先根据layout_width的值来初步指定空间,因为layout_width都是wrap_content, 那布局管理器会分别给两个子控件足够的空间来用于水平方向的拉伸,结果就是下面图片:
  2. 然后,因为水平方向上仍然有足够的空间,那么布局管理器就会将这个多余的控件按照layout_weight的值进行分配。上面的例子中是1:1,那么会将多余控件按照1:1的比例分配,那效果就变成了:
    •   
  3. 如果想给两个控件设置为宽度相同,Android推荐的做法是将两个控件的属性这么设置:layout_width=0dp, layout_weight=1
  4. 如果一个控件没提供layout_weight,那么android会默认其为0
  5. 由于不同的程序员有不同的写代码的惯例,android允许layout_weight的值是小数或整数,分配比例就是各自(layout_weight值)/(各自的layout_weight相加的值),这条规则成立的前提是LinearLayout没有设置android:weightSum来限制总和。

来再看一个经常让人误解的例子:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_height="wrap_content"        android:text="small"        android:textColor="#02F"        android:layout_width="wrap_content"        android:layout_weight="1" />    <TextView        android:layout_height="wrap_content"        android:textColor="#F20"        android:text="A very very long text that needs to wrap.A very very long"        android:layout_width="wrap_content"        android:layout_weight="1" /></LinearLayout>

它的效果是这样:

它并没有按照weight去1:1的分配空间比例,这是因为布局管理器按照第一条规则满足了layout_width=’wrap_content’,这种情况下,如果确实想按照1:1的空间来分配,就只能设置layout_width=’0dp”, 这样做的效果就是这样:

欢迎斧正

更多相关文章

  1. 开源 android 文件管理器
  2. Android嵌套滑动
  3. Android(安卓)自绘输入框
  4. Android仿iPhone 滚轮控件 实现
  5. TabHost和android:layout_height="0.0dip"以及android:layout_we
  6. Android常用控件之ListView(一)
  7. Android周学习Step By Step(5)--常用widget组件
  8. android 点击webView中的按钮修改布局问题
  9. 深入浅出android/ophone UI实现水平布局的Tab控件

随机推荐

  1. 做动态图表,没有数据?用Python就能获取!
  2. 看完这篇文章,我彻底爱上了Python动态图表
  3. NBA投篮数据可视化,4行代码就能实现!
  4. Bong!5 款超牛逼的 Jupyter Notebook 插件
  5. 为什么现在的开发都要懂测试了?没错是的,下
  6. JavaScript 逆向爬取实战(下)
  7. 骚操作!嵌套 JSON 秒变 Dataframe!
  8. PMP证书获得历程
  9. 如何实时主动监控你的网站接口是否挂掉并
  10. 再见 VBA!神器工具统一 Excel 和 Python