Android UI組件LinearLayout線性布局詳解
LinearLayout 線性布局,該布局的繼承關(guān)系:
1. 什么是線性布局
通俗的說感覺起來和線有關(guān),參照線的特點(diǎn),有么是橫向的,要么是豎向的。
LinearLayout是線性布局控件,它包含的子控件將以橫向或豎向的方式排列(通過android:orientation屬性來控制),按照相對位置來排列所有的widgets或者其他的containers,超過邊界時(shí),某些控件將缺失或消失
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:這是布局的唯一標(biāo)識ID
android:orientation:他表示的是這個線性布局是采用橫向還是縱向布局,通常來說只有兩個值:
1. android:orientation=”vertical”表示采用縱向的布局方式,所有在當(dāng)前布局中添加的所有控件都依次按豎向排列
2. android:orientation=”horizontal”表示采用橫向的布局方式,所有在當(dāng)前布局中添加的所有控件都依次按橫向排列(默認(rèn)水平)

android:layout_height: 表示當(dāng)前線性布局的高度
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: 表示當(dāng)前線性布局的寬度
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: 表示所有包含在當(dāng)前布局中的所有控件采用某種方式對齊(默認(rèn)左對齊)
從上依次往下為:
center (垂直且水平居中)
center_horizontal (水平居中)
bottom (底部對齊)
center_vertical (垂直居中)
clip_horizontal (水平方向裁剪,當(dāng)對象邊緣超出容器的時(shí)候,將上下邊緣超出的部分剪切掉,剪切基于縱向?qū)R設(shè)置:頂部對齊時(shí),剪切底部;底部對齊時(shí)剪切頂部;除此之外剪切頂部和底部.)
clip_vertical (垂直方向裁剪,當(dāng)對象邊緣超出容器的時(shí)候,將左右邊緣超出的部分剪切掉,剪切基于橫向?qū)R設(shè)置:左對齊時(shí),剪切右邊部分;右對齊時(shí)剪切左邊部分;除此之外剪切左邊和右邊部分.)
end (放在容器的結(jié)束位置,不改變其大小)
fill (必要的時(shí)候增加對象的橫縱向大小,以完全充滿其容器)
fill_horizontal (必要的時(shí)候增加對象的橫向大小,以完全充滿其容器. 水平方向充)
fill_vertical (必要的時(shí)候增加對象的縱向大小,以完全充滿其容器. 垂直方向填充)
left (將對象放在其容器的左部,不改變其大小)
right (將對象放在其容器的右部,不改變其大小)
start (將對象放在其容器的開始位置,不改變其大小)
top (將對象放在其容器的頂部,不改變其大小)
android:layout_gravity: 表示當(dāng)前線性布局相對于父元素的對齊方式
如上所示
android:background: 表示當(dāng)前線性布局的背景顏色
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)的大小,相當(dā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)整其子的基線。默認(rèn)是true
這里介紹一個小細(xì)節(jié):
這里以垂直分配權(quán)重為列,權(quán)重是可以為負(fù)數(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,這樣就形成了一種想反的效果,相當(dāng)于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開發(fā)實(shí)現(xiàn)的圖片點(diǎn)擊切換功能示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)的圖片點(diǎn)擊切換功能,涉及Android ImageView組件創(chuàng)建、布局及實(shí)現(xiàn)圖形切換相關(guān)操作技巧,需要的朋友可以參考下2019-04-04
Android自定義View實(shí)現(xiàn)飄動的葉子效果(三)
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)飄動的葉子效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android編程實(shí)現(xiàn)在底端顯示選項(xiàng)卡的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)在底端顯示選項(xiàng)卡的方法,涉及Android界面線性布局、相對布局及選項(xiàng)卡設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
Android design包自定義tablayout的底部導(dǎo)航欄的實(shí)現(xiàn)方法
這篇文章主要介紹了Android design包自定義tablayout的底部導(dǎo)航欄的實(shí)現(xiàn)方法,非常不錯,具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01

