Android中的android:layout_weight使用詳解
更新時(shí)間:2013年06月16日 16:20:07 作者:
layout_weight的作用是設(shè)置子空間在LinearLayout的重要度(控件的大小比重)。layout_weight的值越低,則控件越重要,下面為大家介紹下具體的使用方法
在使用LinearLayout的時(shí)候,子控件可以設(shè)置layout_weight。layout_weight的作用是設(shè)置子空間在LinearLayout的重要度(控件的大小比重)。layout_weight的值越低,則控件越重要。若不設(shè)置layout_weight則默認(rèn)比重為0。
如果在一個(gè)LinearLayout里面放置兩個(gè)Button,Button1和Button2,Button1的layout_weight設(shè)置為1,Button2的layout_weight設(shè)置為2,且兩個(gè)Button的layout_width都設(shè)置為fill_parent。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2"/>
</LinearLayout>
則Button1占據(jù)屏幕寬度的三分之二,而Button2占據(jù)三分之一,如下圖所示:
如果兩個(gè)Button的layout_width都設(shè)置成wrap_content,則情況剛好相反。Button1占三分之一,Button2占三分之二,如下圖所示:
layout_weight在使用LinearLayout設(shè)計(jì)復(fù)雜的布局時(shí)還是挺有用處的,例如,在水平的線性布局中,你要分足夠的空間給控件1,剩下的空間則分配給控件2,則只要設(shè)置控件1的layout_width設(shè)置為wrap_content,不用設(shè)置layout_weight,而在控件2中,設(shè)置layout_width為fill_parent,layout_weight為1即可實(shí)現(xiàn)。
如果在一個(gè)LinearLayout里面放置兩個(gè)Button,Button1和Button2,Button1的layout_weight設(shè)置為1,Button2的layout_weight設(shè)置為2,且兩個(gè)Button的layout_width都設(shè)置為fill_parent。
復(fù)制代碼 代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2"/>
</LinearLayout>
則Button1占據(jù)屏幕寬度的三分之二,而Button2占據(jù)三分之一,如下圖所示:
如果兩個(gè)Button的layout_width都設(shè)置成wrap_content,則情況剛好相反。Button1占三分之一,Button2占三分之二,如下圖所示:
layout_weight在使用LinearLayout設(shè)計(jì)復(fù)雜的布局時(shí)還是挺有用處的,例如,在水平的線性布局中,你要分足夠的空間給控件1,剩下的空間則分配給控件2,則只要設(shè)置控件1的layout_width設(shè)置為wrap_content,不用設(shè)置layout_weight,而在控件2中,設(shè)置layout_width為fill_parent,layout_weight為1即可實(shí)現(xiàn)。
您可能感興趣的文章:
- jQuery布局插件UI Layout簡(jiǎn)介及使用方法
- android layout 按比例布局的代碼
- 基于AnDroid FrameLayout的使用詳解
- jQuery EasyUI 中文API Layout(Tabs)
- SWT(JFace)體驗(yàn)之GridLayout布局
- android LinearLayout和RelativeLayout組合實(shí)現(xiàn)精確布局方法介紹
- jqeury-easyui-layout問題解決方法
- Android布局——Preference自定義layout的方法
- Android開發(fā)筆記 TableLayout常用的屬性介紹
- CoordinatorLayout的使用如此簡(jiǎn)單(Android)
相關(guān)文章
Android 使用PopupWindow實(shí)現(xiàn)彈出更多的菜單實(shí)例詳解
最近想要做一個(gè)彈出更多的菜單,而原生的彈出菜單卻不是我們想要的效果,所以必然要自定義菜單。接下來通過本文給大家介紹android 使用popupwindow實(shí)現(xiàn)彈出更多的菜單實(shí)例詳解,需要的朋友可以參考下2017-04-04
Android編程實(shí)現(xiàn)捕獲程序異常退出時(shí)的錯(cuò)誤log信息功能詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)捕獲程序異常退出時(shí)的錯(cuò)誤log信息功能,結(jié)合實(shí)例形式分析了Android異常信息捕獲與日志操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-08-08
Android 利用三階貝塞爾曲線繪制運(yùn)動(dòng)軌跡的示例
本篇文章主要介紹了Android 利用三階貝塞爾曲線繪制運(yùn)動(dòng)軌跡的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
Android使用多線程實(shí)現(xiàn)斷點(diǎn)下載
這篇文章主要介紹了Android使用多線程實(shí)現(xiàn)斷點(diǎn)下載,多線程下載是加快下載速度的一種方式,感興趣的小伙伴們可以參考一下2016-03-03
Android實(shí)現(xiàn)簡(jiǎn)單水波紋效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單水波紋效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
詳解Android ScrollView嵌套EditText出現(xiàn)的滑動(dòng)問題
本篇文章主要介紹了詳解ScrollView嵌套EditText出現(xiàn)的滑動(dòng)問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
2018-01-01
Android Activity之間傳遞圖片(Bitmap)的方法
這篇文章介紹了Android Activity之間傳遞圖片(Bitmap)的方法,有需要的朋友可以參考一下
2013-08-08 
