Android中的android:layout_weight使用詳解
更新時間:2013年06月16日 16:20:07 作者:
layout_weight的作用是設置子空間在LinearLayout的重要度(控件的大小比重)。layout_weight的值越低,則控件越重要,下面為大家介紹下具體的使用方法
在使用LinearLayout的時候,子控件可以設置layout_weight。layout_weight的作用是設置子空間在LinearLayout的重要度(控件的大小比重)。layout_weight的值越低,則控件越重要。若不設置layout_weight則默認比重為0。
如果在一個LinearLayout里面放置兩個Button,Button1和Button2,Button1的layout_weight設置為1,Button2的layout_weight設置為2,且兩個Button的layout_width都設置為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占據屏幕寬度的三分之二,而Button2占據三分之一,如下圖所示:
如果兩個Button的layout_width都設置成wrap_content,則情況剛好相反。Button1占三分之一,Button2占三分之二,如下圖所示:
layout_weight在使用LinearLayout設計復雜的布局時還是挺有用處的,例如,在水平的線性布局中,你要分足夠的空間給控件1,剩下的空間則分配給控件2,則只要設置控件1的layout_width設置為wrap_content,不用設置layout_weight,而在控件2中,設置layout_width為fill_parent,layout_weight為1即可實現。
如果在一個LinearLayout里面放置兩個Button,Button1和Button2,Button1的layout_weight設置為1,Button2的layout_weight設置為2,且兩個Button的layout_width都設置為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占據屏幕寬度的三分之二,而Button2占據三分之一,如下圖所示:
如果兩個Button的layout_width都設置成wrap_content,則情況剛好相反。Button1占三分之一,Button2占三分之二,如下圖所示:
layout_weight在使用LinearLayout設計復雜的布局時還是挺有用處的,例如,在水平的線性布局中,你要分足夠的空間給控件1,剩下的空間則分配給控件2,則只要設置控件1的layout_width設置為wrap_content,不用設置layout_weight,而在控件2中,設置layout_width為fill_parent,layout_weight為1即可實現。
您可能感興趣的文章:
- jQuery布局插件UI Layout簡介及使用方法
- android layout 按比例布局的代碼
- 基于AnDroid FrameLayout的使用詳解
- jQuery EasyUI 中文API Layout(Tabs)
- SWT(JFace)體驗之GridLayout布局
- android LinearLayout和RelativeLayout組合實現精確布局方法介紹
- jqeury-easyui-layout問題解決方法
- Android布局——Preference自定義layout的方法
- Android開發(fā)筆記 TableLayout常用的屬性介紹
- CoordinatorLayout的使用如此簡單(Android)
相關文章
Android 使用PopupWindow實現彈出更多的菜單實例詳解
最近想要做一個彈出更多的菜單,而原生的彈出菜單卻不是我們想要的效果,所以必然要自定義菜單。接下來通過本文給大家介紹android 使用popupwindow實現彈出更多的菜單實例詳解,需要的朋友可以參考下2017-04-04
Android編程實現捕獲程序異常退出時的錯誤log信息功能詳解
這篇文章主要介紹了Android編程實現捕獲程序異常退出時的錯誤log信息功能,結合實例形式分析了Android異常信息捕獲與日志操作相關實現技巧,需要的朋友可以參考下2017-08-08
詳解Android ScrollView嵌套EditText出現的滑動問題
本篇文章主要介紹了詳解ScrollView嵌套EditText出現的滑動問題,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
Android Activity之間傳遞圖片(Bitmap)的方法
這篇文章介紹了Android Activity之間傳遞圖片(Bitmap)的方法,有需要的朋友可以參考一下2013-08-08

