android:layout_weight属性详解

 在android开发中LinearLayout很经常使用,LinearLayout的内控件的android:layout_weight在某些场景显得很是重要,好比咱们须要按比例显示。android并没用提供table这样的控件,虽然有TableLayout,可是它并不是是咱们想象中的像html里面的table那么好用,咱们经常使用ListView实现table的效果,可是列对齐确比较麻烦,如今用LinearLayout及属性android:layout_weight能很好地解决。下面咱们共同体验下layout_weight这个属性。html

  1、LinearLayout内的控件的layout_width设置为"wrap_content",请看一下xml配置:android

 
<LinearLayout

      android:orientation="horizontal"

      android:layout_width="fill_parent"

      android:layout_height="fill_parent"

      android:layout_weight="1"

     >

      <TextView

          android:layout_width="wrap_content"

          android:layout_height="fill_parent"

          android:layout_weight="1"

          android:background="#aa0000"

          android:gravity="center"

          android:text="1"/>

      <TextView

          android:layout_width="wrap_content"

          android:layout_height="fill_parent"

          android:layout_weight="2"

          android:background="#00aa00"

          android:gravity="center"

          android:text="1"/>

      <TextView

          android:layout_width="wrap_content"

          android:layout_height="fill_parent"

          android:layout_weight="3"

          android:background="#0000aa"

          android:gravity="center"

          android:text="1"/>

  </LinearLayout>

 效果以下:web

能够看到这三个TextView是按照1:2:3的比例进行显示的,这样看来彷佛能够实现按照比例显示了,可是有个问题,若是TextView内的文本长度一同那么较长文本的TextView会宽度会有所增长,见下面配置及效果:框架

配置:google

 
 <LinearLayout

      android:orientation="horizontal"

      android:layout_width="fill_parent"

      android:layout_height="fill_parent"

      android:layout_weight="1">

      <TextView

          android:layout_width="wrap_content"

          android:layout_height="fill_parent"

          android:layout_weight="1"

          android:background="#aa0000"

          android:gravity="center"

          android:text="1111111111111111111111111111111111111111111"/>

      <TextView

          android:layout_width="wrap_content"

          android:layout_height="fill_parent"

          android:layout_weight="2"

          android:background="#00aa00"

          android:gravity="center"

          android:text="2"/>

      <TextView

          android:layout_width="wrap_content"

          android:layout_height="fill_parent"

          android:layout_weight="3"

          android:background="#0000aa"

          android:gravity="center"

          android:text="3"/>

  </LinearLayout>

效果:spa

这样看来咱们所须要的按比例又没法实现了,通过满天地google终于找到了解决方案,就是设置layout_width设置为"wrap_content"。配置及效果见下:设计

 
 <LinearLayout

      android:orientation="horizontal"

      android:layout_width="fill_parent"

      android:layout_height="fill_parent"

      android:layout_weight="1">

      <TextView

          android:layout_width="0dp"

          android:layout_height="fill_parent"

          android:layout_weight="1"

          android:background="#aa0000"

          android:gravity="center"

          android:text="1111111111111111111111111111111111111111111"/>

      <TextView

          android:layout_width="0dp"

          android:layout_height="fill_parent"

          android:layout_weight="2"

          android:background="#00aa00"

          android:gravity="center"

          android:text="2"/>

      <TextView

          android:layout_width="0dp"

          android:layout_height="fill_parent"

          android:layout_weight="3"

          android:background="#0000aa"

          android:gravity="center"

          android:text="3"/>

  </LinearLayout>

效果:code

这样终于达到咱们的按比例显示的效果了,感受非常奇怪,android开发框架的大佬们有时候设计确实有点匪夷所思。orm

  2、LinearLayout内的控件的layout_width设置为"fill_parent",请看一下xml配置:xml

 
 <LinearLayout

      android:orientation="horizontal"

      android:layout_width="fill_parent"

      android:layout_height="fill_parent"

      android:layout_weight="1">

      <TextView

          android:layout_width="fill_parent"

          android:layout_height="fill_parent"

          android:layout_weight="1"

          android:background="#aa0000"

          android:gravity="center"

          android:text="1"/>

      <TextView

          android:layout_width="fill_parent"

          android:layout_height="fill_parent"

          android:layout_weight="2"

          android:background="#00aa00"

          android:gravity="center"

          android:text="2"/>

  </LinearLayout>


效果以下:

奇怪吧,整个宽度平分3块,第一个TextView占了两块,这样看来weight值越小的优先级越大。只有两个TextView彷佛看出些道理,那么让咱们看看三个是什么效果:

  <LinearLayout

      android:orientation="horizontal"

      android:layout_width="fill_parent"

      android:layout_height="fill_parent"

      android:layout_weight="1">

      <TextView

          android:layout_width="fill_parent"

          android:layout_height="fill_parent"

          android:layout_weight="1"

          android:background="#aa0000"

          android:gravity="center"

          android:text="1"/>

      <TextView

          android:layout_width="fill_parent"

          android:layout_height="fill_parent"

          android:layout_weight="2"

          android:background="#00aa00"

          android:gravity="center"

          android:text="2"/>

      <TextView

          android:layout_width="fill_parent"

          android:layout_height="fill_parent"

          android:layout_weight="3"

          android:background="#0000aa"

          android:gravity="center"

          android:text="3"/>

  </LinearLayout>

效果:

什么意思?第三个TextView丢掉了,非常奇怪,让咱们再试一个,把weight分别改成2,3,4的看看效果:

这个效果让人很困惑,我一直想寻求出一个确切的比例公式,可是至今未找到。有哪位大神能搞定的话忘不吝赐教。

虽然这个android:layout_weight属性很怪异,但幸运的是咱们达到了目标:

  按比例显示LinearLayout内各个子控件,需设置android:layout_width="0dp",若是为竖直方向的设置android:layout_height="0dp"。在这种状况下某子个控件占用LinearLayout的比例为:本控件weight值 / LinearLayout内全部控件的weight值的和。

相关文章
相关标签/搜索