Android中的android:layout_weight使用詳解
更新時間:2013年06月16日 16:20:07 作者:
layout_weight的作用是設(shè)置子空間在LinearLayout的重要度(控件的大小比重)。layout_weight的值越低,則控件越重要,下面為大家介紹下具體的使用方法
在使用LinearLayout的時候,子控件可以設(shè)置layout_weight。layout_weight的作用是設(shè)置子空間在LinearLayout的重要度(控件的大小比重)。layout_weight的值越低,則控件越重要。若不設(shè)置layout_weight則默認比重為0。
如果在一個LinearLayout里面放置兩個Button,Button1和Button2,Button1的layout_weight設(shè)置為1,Button2的layout_weight設(shè)置為2,且兩個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ù)三分之一,如下圖所示:
如果兩個Button的layout_width都設(shè)置成wrap_content,則情況剛好相反。Button1占三分之一,Button2占三分之二,如下圖所示:
layout_weight在使用LinearLayout設(shè)計復(fù)雜的布局時還是挺有用處的,例如,在水平的線性布局中,你要分足夠的空間給控件1,剩下的空間則分配給控件2,則只要設(shè)置控件1的layout_width設(shè)置為wrap_content,不用設(shè)置layout_weight,而在控件2中,設(shè)置layout_width為fill_parent,layout_weight為1即可實現(xiàn)。
如果在一個LinearLayout里面放置兩個Button,Button1和Button2,Button1的layout_weight設(shè)置為1,Button2的layout_weight設(shè)置為2,且兩個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ù)三分之一,如下圖所示:

如果兩個Button的layout_width都設(shè)置成wrap_content,則情況剛好相反。Button1占三分之一,Button2占三分之二,如下圖所示:

layout_weight在使用LinearLayout設(shè)計復(fù)雜的布局時還是挺有用處的,例如,在水平的線性布局中,你要分足夠的空間給控件1,剩下的空間則分配給控件2,則只要設(shè)置控件1的layout_width設(shè)置為wrap_content,不用設(shè)置layout_weight,而在控件2中,設(shè)置layout_width為fill_parent,layout_weight為1即可實現(xiàn)。
您可能感興趣的文章:
- jQuery布局插件UI Layout簡介及使用方法
- android layout 按比例布局的代碼
- 基于AnDroid FrameLayout的使用詳解
- jQuery EasyUI 中文API Layout(Tabs)
- SWT(JFace)體驗之GridLayout布局
- android LinearLayout和RelativeLayout組合實現(xiàn)精確布局方法介紹
- jqeury-easyui-layout問題解決方法
- Android布局——Preference自定義layout的方法
- Android開發(fā)筆記 TableLayout常用的屬性介紹
- CoordinatorLayout的使用如此簡單(Android)
相關(guān)文章
Android百度地圖實現(xiàn)搜索和定位及自定義圖標繪制并點擊時彈出泡泡
這篇文章主要介紹了Android百度地圖實現(xiàn)搜索和定位及自定義圖標繪制并點擊時彈出泡泡的相關(guān)資料,需要的朋友可以參考下2016-01-01Android開發(fā)之圖形圖像與動畫(一)Paint和Canvas類學(xué)習(xí)
Paint類代表畫筆,用來描述圖形的顏色和風格,如線寬,顏色,透明度和填充效果等信息;Canvas類代表畫布,通過該類提供的構(gòu)造方法,可以繪制各種圖形;感興趣的朋友可以了解下啊,希望本文對你有所幫助2013-01-01Android中通過Notification&NotificationManager實現(xiàn)消息通知
關(guān)于通知Notification相信大家都不陌生了,平時上QQ的時候有消息來了或者有收到了短信,手機頂部就會顯示有新消息什么的,就類似這種。今天就稍微記錄下幾種Notification的用法。3.0以前的通知和3.0以后的通知是有些區(qū)別的。2015-10-10根據(jù)USER-AGENT判斷手機類型并跳轉(zhuǎn)到相應(yīng)的app下載頁面
檢測瀏覽器的USER-AGENT,然后根據(jù)正則表達式來確定客戶端類型,并跳轉(zhuǎn)到相應(yīng)的app下載頁面,這個方法還是比較實用的,大家可以看看2014-09-09