android:layout_weight屬性詳解

在android開發(fā)中LinearLayout很常用,LinearLayout的內(nèi)控件的android:layout_weight在某些場(chǎng)景顯得非常重要,比如我們需要按比例顯示。android并沒(méi)用提供table這樣的控件,雖然有TableLayout,但是它并非是我們想象中的像html里面的table那么好用,我們常用ListView實(shí)現(xiàn)table的效果,但是列對(duì)齊確比較麻煩,現(xiàn)在用LinearLayout及屬性android:layout_weight能很好地解決。下面我們共同體驗(yàn)下layout_weight這個(gè)屬性。  
一、LinearLayout內(nèi)的控件的layout_width設(shè)置為"wrap_content",請(qǐng)看一下xml配置:

 <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>

** 效果如下:**


android:layout_weight屬性詳解

可以看到這三個(gè)TextView是按照1:2:3的比例進(jìn)行顯示的,這樣看來(lái)似乎可以實(shí)現(xiàn)按照比例顯示了,但是有個(gè)問(wèn)題,如果TextView內(nèi)的文本長(zhǎng)度一同那么較長(zhǎng)文本的TextView會(huì)寬度會(huì)有所增加,見下面配置及效果:
配置:

<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>

效果:


android:layout_weight屬性詳解

這樣看來(lái)我們所需要的按比例又無(wú)法實(shí)現(xiàn)了,經(jīng)過(guò)滿天地google終于找到了解決方案,就是設(shè)置layout_width設(shè)置為"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>

效果:

android:layout_weight屬性詳解

這樣終于達(dá)到我們的按比例顯示的效果了,感覺很是奇怪,android開發(fā)框架的大佬們有時(shí)候設(shè)計(jì)確實(shí)有點(diǎn)匪夷所思。
  二、LinearLayout內(nèi)的控件的layout_width設(shè)置為"fill_parent",請(qǐng)看一下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>

效果如下:


android:layout_weight屬性詳解

奇怪吧,整個(gè)寬度平分3塊,第一個(gè)TextView占了兩塊,這樣看來(lái)weight值越小的優(yōu)先級(jí)越大。只有兩個(gè)TextView似乎看出些道理,那么讓我們看看三個(gè)是什么效果:

<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>

效果:

android:layout_weight屬性詳解

什么意思?第三個(gè)TextView丟掉了,很是奇怪,讓我們?cè)僭囈粋€(gè),把weight分別改為2,3,4的看看效果:
android:layout_weight屬性詳解

這個(gè)效果讓人很困惑,我一直想尋求出一個(gè)確切的比例公式,但是至今未找到。有哪位大神能搞定的話忘不吝賜教。
雖然這個(gè)android:layout_weight屬性很怪異,但幸運(yùn)的是我們達(dá)到了目標(biāo):
  按比例顯示LinearLayout內(nèi)各個(gè)子控件,需設(shè)置android:layout_width="0dp",如果為豎直方向的設(shè)置android:layout_height="0dp"。在這種情況下某子個(gè)控件占用LinearLayout的比例為:本控件weight值 / LinearLayout內(nèi)所有控件的weight值的和。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容