Android UI組件LinearLayout線性布局詳解
LinearLayout 線性布局,該布局的繼承關(guān)系:
1. 什么是線性布局
通俗的說感覺起來和線有關(guān),參照線的特點,有么是橫向的,要么是豎向的。
LinearLayout是線性布局控件,它包含的子控件將以橫向或豎向的方式排列(通過android:orientation屬性來控制),按照相對位置來排列所有的widgets或者其他的containers,超過邊界時,某些控件將缺失或消失
2. 線性布局常用基本屬性
- android:id
- android:orientation
- android:layout_height
- android:layout_width
- android:gravity
- android:layout_gravity
- android:background
- android:layout_margin:
- android:padding
- android:weightSum
- android:layout_weight
- android:baselineAligned
3. 常用屬性值介紹:
android:id:這是布局的唯一標識ID
android:orientation:他表示的是這個線性布局是采用橫向還是縱向布局,通常來說只有兩個值:
1. android:orientation=”vertical”表示采用縱向的布局方式,所有在當前布局中添加的所有控件都依次按豎向排列
2. android:orientation=”horizontal”表示采用橫向的布局方式,所有在當前布局中添加的所有控件都依次按橫向排列(默認水平)
android:layout_height: 表示當前線性布局的高度
4. android:layout_height="match_parent" (表示高度占滿整個屏幕)
5. android:layout_height="wrap_content" (表示高度根據(jù)其包含的控件自適應(yīng)調(diào)整)
6. android:layout_height="30dp"(自定義設(shè)置高度,通常單位為dp)
android:layout_width: 表示當前線性布局的寬度
7. android:layout_width="match_parent" (表示寬度占滿整個屏幕)
8. android:layout_width="wrap_content" (表示寬度根據(jù)其包含的控件自適應(yīng)調(diào)整)
9. android:layout_width="30dp"(自定義設(shè)置寬度,通常單位為dp)
android:gravity: 表示所有包含在當前布局中的所有控件采用某種方式對齊(默認左對齊)
從上依次往下為:
center (垂直且水平居中)
center_horizontal (水平居中)
bottom (底部對齊)
center_vertical (垂直居中)
clip_horizontal (水平方向裁剪,當對象邊緣超出容器的時候,將上下邊緣超出的部分剪切掉,剪切基于縱向?qū)R設(shè)置:頂部對齊時,剪切底部;底部對齊時剪切頂部;除此之外剪切頂部和底部.)
clip_vertical (垂直方向裁剪,當對象邊緣超出容器的時候,將左右邊緣超出的部分剪切掉,剪切基于橫向?qū)R設(shè)置:左對齊時,剪切右邊部分;右對齊時剪切左邊部分;除此之外剪切左邊和右邊部分.)
end (放在容器的結(jié)束位置,不改變其大小)
fill (必要的時候增加對象的橫縱向大小,以完全充滿其容器)
fill_horizontal (必要的時候增加對象的橫向大小,以完全充滿其容器. 水平方向充)
fill_vertical (必要的時候增加對象的縱向大小,以完全充滿其容器. 垂直方向填充)
left (將對象放在其容器的左部,不改變其大小)
right (將對象放在其容器的右部,不改變其大小)
start (將對象放在其容器的開始位置,不改變其大小)
top (將對象放在其容器的頂部,不改變其大小)
android:layout_gravity: 表示當前線性布局相對于父元素的對齊方式
如上所示
android:background: 表示當前線性布局的背景顏色
android:margin:表示外邊距,通常表示本控件與父控件四面之間的距離
從上往下:
1. 底邊距
2. 與控件結(jié)尾的邊距
3. 左邊距
4. 右邊距
5. 與控件的起始邊距
6. 頂邊距
android:padding:表示內(nèi)邊距,通常表示是本元素所有子元素的與父元素邊緣的距離,設(shè)置在父元素上,比如文字與文本控件的所有距離
從上往下如上所示
android:weightSum:權(quán)重的總比例
android:layout_weight:子元素對未占用空間水平或垂直分布的權(quán)重
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.qianfeng.demo1.MainActivity" android:weightSum="6" //總的權(quán)重為6 android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2"http://此控件占用比例為2,其他控件全部占用1,超過比例的權(quán)重都不會分配對應(yīng)的大小,相當于大小為0 android:textSize="30sp" android:text="aaaa" android:background="#f00" p /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textSize="30sp" android:text="bbbb" android:background="#0f0" /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textSize="30sp" android:background="#00f" android:text="cccc" /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textSize="30sp" android:text="dddd" android:background="#0ff" /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textSize="30sp" android:background="#f0f" android:text="eeee" /> </LinearLayout>
效果圖如下:
android:baselineAligned:該控件只對能顯示text的子控件有效。
這必須是一個布爾值,要么“true”或“false”,并防止布局調(diào)整其子的基線。默認是true
這里介紹一個小細節(jié):
這里以垂直分配權(quán)重為列,權(quán)重是可以為負數(shù)的,權(quán)重是根據(jù)比例分配對應(yīng)大小,這里aaaa控件的高為0dp,bbbb控件的高為0dp,所以,aaaa控件分配權(quán)重的是5,bbbb分配權(quán)重的是1,所以這里aaaa會比bbbb占用比例大。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.qianfeng.demo1.MainActivity" android:weightSum="6" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="5" android:textSize="30sp" android:text="aaaa" android:background="#f00" /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textSize="30sp" android:text="bbbb" android:background="#0f0" /> </LinearLayout>
效果圖:
反之,如果設(shè)置了aaaa控件的高為match_parent, bbbb控件的高也為match_parent,aaaa控件分配權(quán)重的是5,bbbb分配權(quán)重的是1,這樣就形成了一種想反的效果,相當于aaaa控件分配的是-5 bbbb控件分配的是-1 而-1 比 -5大,所以,對應(yīng)bbbb控件占用的比例比aaaa大
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.qianfeng.demo1.MainActivity" android:weightSum="6" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="5" android:textSize="30sp" android:text="aaaa" android:background="#f00" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:textSize="30sp" android:text="bbbb" android:background="#0f0" /> </LinearLayout>
效果圖如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android style的繼承方式 點(.)和parent詳解及實例
這篇文章主要介紹了Android style的繼承方式 點(.)和parent詳解及實例的相關(guān)資料,需要的朋友可以參考下2017-02-02Android基于ImageView繪制的開關(guān)按鈕效果示例
這篇文章主要介紹了Android基于ImageView繪制的開關(guān)按鈕效果,結(jié)合實例形式分析了Android使用ImageView進行按鈕繪制的界面布局、功能實現(xiàn)及相關(guān)注意事項,需要的朋友可以參考下2017-03-03android在連拍菜單中增加連拍張數(shù)選項功能實現(xiàn)代碼
想要增加連拍張數(shù)選項需要在entries, entryvalues中添加兩項,同時在mtk_strings.xml中添加相應(yīng)的字符串,具體如下,感興趣的朋友可以參考下哈2013-06-06實例講解Android app開發(fā)中ListView的基本使用及優(yōu)化
這篇文章主要介紹了Android app開發(fā)中ListView的基本使用及優(yōu)化,ListView視圖組件是Android中最常用的組件之一需要的朋友可以參考下2016-02-02listView的item中有checkbox,導(dǎo)致setOnItemClick失效的原因及解決辦法
這篇文章主要介紹了listView的item中有checkbox,導(dǎo)致setOnItemClick失效的原因及解決辦法,需要的朋友可以參考下2017-01-01