Android UI組件LinearLayout線性布局詳解
LinearLayout 線性布局,該布局的繼承關(guān)系:
1. 什么是線性布局
通俗的說(shuō)感覺(jué)起來(lái)和線有關(guān),參照線的特點(diǎn),有么是橫向的,要么是豎向的。
LinearLayout是線性布局控件,它包含的子控件將以橫向或豎向的方式排列(通過(guò)android:orientation屬性來(lái)控制),按照相對(duì)位置來(lái)排列所有的widgets或者其他的containers,超過(guò)邊界時(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)識(shí)ID
android:orientation:他表示的是這個(gè)線性布局是采用橫向還是縱向布局,通常來(lái)說(shuō)只有兩個(gè)值:
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" (表示高度占滿(mǎn)整個(gè)屏幕)
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" (表示寬度占滿(mǎn)整個(gè)屏幕)
8. android:layout_width="wrap_content" (表示寬度根據(jù)其包含的控件自適應(yīng)調(diào)整)
9. android:layout_width="30dp"(自定義設(shè)置寬度,通常單位為dp)
android:gravity: 表示所有包含在當(dāng)前布局中的所有控件采用某種方式對(duì)齊(默認(rèn)左對(duì)齊)
從上依次往下為:
center (垂直且水平居中)
center_horizontal (水平居中)
bottom (底部對(duì)齊)
center_vertical (垂直居中)
clip_horizontal (水平方向裁剪,當(dāng)對(duì)象邊緣超出容器的時(shí)候,將上下邊緣超出的部分剪切掉,剪切基于縱向?qū)R設(shè)置:頂部對(duì)齊時(shí),剪切底部;底部對(duì)齊時(shí)剪切頂部;除此之外剪切頂部和底部.)
clip_vertical (垂直方向裁剪,當(dāng)對(duì)象邊緣超出容器的時(shí)候,將左右邊緣超出的部分剪切掉,剪切基于橫向?qū)R設(shè)置:左對(duì)齊時(shí),剪切右邊部分;右對(duì)齊時(shí)剪切左邊部分;除此之外剪切左邊和右邊部分.)
end (放在容器的結(jié)束位置,不改變其大小)
fill (必要的時(shí)候增加對(duì)象的橫縱向大小,以完全充滿(mǎn)其容器)
fill_horizontal (必要的時(shí)候增加對(duì)象的橫向大小,以完全充滿(mǎn)其容器. 水平方向充)
fill_vertical (必要的時(shí)候增加對(duì)象的縱向大小,以完全充滿(mǎn)其容器. 垂直方向填充)
left (將對(duì)象放在其容器的左部,不改變其大小)
right (將對(duì)象放在其容器的右部,不改變其大小)
start (將對(duì)象放在其容器的開(kāi)始位置,不改變其大小)
top (將對(duì)象放在其容器的頂部,不改變其大小)
android:layout_gravity: 表示當(dāng)前線性布局相對(duì)于父元素的對(duì)齊方式
如上所示
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:子元素對(duì)未占用空間水平或垂直分布的權(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,超過(guò)比例的權(quán)重都不會(huì)分配對(duì)應(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:該控件只對(duì)能顯示text的子控件有效。
這必須是一個(gè)布爾值,要么“true”或“false”,并防止布局調(diào)整其子的基線。默認(rèn)是true
這里介紹一個(gè)小細(xì)節(jié):
這里以垂直分配權(quán)重為列,權(quán)重是可以為負(fù)數(shù)的,權(quán)重是根據(jù)比例分配對(duì)應(yīng)大小,這里aaaa控件的高為0dp,bbbb控件的高為0dp,所以,aaaa控件分配權(quán)重的是5,bbbb分配權(quán)重的是1,所以這里aaaa會(huì)比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大,所以,對(duì)應(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>
效果圖如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義view實(shí)現(xiàn)輸入框效果
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)輸入框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03Android style的繼承方式 點(diǎn)(.)和parent詳解及實(shí)例
這篇文章主要介紹了Android style的繼承方式 點(diǎn)(.)和parent詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02Android基于ImageView繪制的開(kāi)關(guān)按鈕效果示例
這篇文章主要介紹了Android基于ImageView繪制的開(kāi)關(guān)按鈕效果,結(jié)合實(shí)例形式分析了Android使用ImageView進(jìn)行按鈕繪制的界面布局、功能實(shí)現(xiàn)及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-03-03android在連拍菜單中增加連拍張數(shù)選項(xiàng)功能實(shí)現(xiàn)代碼
想要增加連拍張數(shù)選項(xiàng)需要在entries, entryvalues中添加兩項(xiàng),同時(shí)在mtk_strings.xml中添加相應(yīng)的字符串,具體如下,感興趣的朋友可以參考下哈2013-06-06android 開(kāi)發(fā)教程之日歷項(xiàng)目實(shí)踐(三)
決定開(kāi)始學(xué)習(xí) Android 平臺(tái)下的軟件開(kāi)發(fā),以日歷作為實(shí)踐項(xiàng)目,進(jìn)行一周后,基本完成,有需要的朋友可以參考下2013-01-01Cocos2d-x入門(mén)教程(詳細(xì)的實(shí)例和講解)
這篇文章主要介紹了Cocos2d-x入門(mén)教程,包括詳細(xì)的實(shí)例、講解以及實(shí)現(xiàn)過(guò)程,需要的朋友可以參考下2014-04-04實(shí)例講解Android app開(kāi)發(fā)中ListView的基本使用及優(yōu)化
這篇文章主要介紹了Android app開(kāi)發(fā)中ListView的基本使用及優(yōu)化,ListView視圖組件是Android中最常用的組件之一需要的朋友可以參考下2016-02-02listView的item中有checkbox,導(dǎo)致setOnItemClick失效的原因及解決辦法
這篇文章主要介紹了listView的item中有checkbox,導(dǎo)致setOnItemClick失效的原因及解決辦法,需要的朋友可以參考下2017-01-01Android身份證號(hào)有效性校驗(yàn)工具類(lèi)案例
這篇文章主要介紹了Android身份證號(hào)有效性校驗(yàn)工具類(lèi)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09