android layout_width 属性,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配置:
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#aa0000"
android:gravity="center"
android:text="1"/>
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00aa00"
android:gravity="center"
android:text="1"/>
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="3"
android:background="#0000aa"
android:gravity="center"
android:text="1"/>
** 效果如下:**
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ì)有所增加,見(jiàn)下面配置及效果:
配置:
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#aa0000"
android:gravity="center"
android:text="1111111111111111111111111111111111111111111"/>
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00aa00"
android:gravity="center"
android:text="2"/>
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="3"
android:background="#0000aa"
android:gravity="center"
android:text="3"/>
效果:
android:layout_weight屬性詳解
這樣看來(lái)我們所需要的按比例又無(wú)法實(shí)現(xiàn)了,經(jīng)過(guò)滿天地google終于找到了解決方案,就是設(shè)置layout_width設(shè)置為"wrap_content"。配置及效果見(jiàn)下:
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#aa0000"
android:gravity="center"
android:text="1111111111111111111111111111111111111111111"/>
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00aa00"
android:gravity="center"
android:text="2"/>
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="3"
android:background="#0000aa"
android:gravity="center"
android:text="3"/>
效果:
android:layout_weight屬性詳解
這樣終于達(dá)到我們的按比例顯示的效果了,感覺(jué)很是奇怪,android開發(fā)框架的大佬們有時(shí)候設(shè)計(jì)確實(shí)有點(diǎn)匪夷所思。
二、LinearLayout內(nèi)的控件的layout_width設(shè)置為"fill_parent",請(qǐng)看一下xml配置:
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#aa0000"
android:gravity="center"
android:text="1"/>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00aa00"
android:gravity="center"
android:text="2"/>
效果如下:
android:layout_weight屬性詳解
奇怪吧,整個(gè)寬度平分3塊,第一個(gè)TextView占了兩塊,這樣看來(lái)weight值越小的優(yōu)先級(jí)越大。只有兩個(gè)TextView似乎看出些道理,那么讓我們看看三個(gè)是什么效果:
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#aa0000"
android:gravity="center"
android:text="1"/>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00aa00"
android:gravity="center"
android:text="2"/>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:background="#0000aa"
android:gravity="center"
android:text="3"/>
效果:
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值的和。
總結(jié)
以上是生活随笔為你收集整理的android layout_width 属性,android:layout_weight属性详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux文件什么权限比较安全,linu
- 下一篇: yii+php+当前目录,Yii应用的目