寫在前面的話
<p>
LinearLayout對于開發(fā)來說羡宙,是使用最常用的布局控件之一,但是對于LinearLayout我們究竟有多了解呢掐隐?最近在看LinearLayout的源碼狗热,看源碼過程中發(fā)現(xiàn)其實(shí)有很多東西自己并沒有使用到,對于LinearLayout的了解也是并沒有那么足虑省,那么這篇文章就對LinearLayout進(jìn)行更加詳細(xì)與深入的了解匿刮,從使用到源碼,一一進(jìn)行解析探颈!
一.LinearLayout屬性
<p>
1)基準(zhǔn)線對齊
<p>
xml屬性 : android:baselineAligned;
設(shè)置方法 : setBaselineAligned(boolean b);
作用 : 如果該屬性為false, 就會(huì)阻止該布局管理器與其子元素的基準(zhǔn)線對齊;
要知道這個(gè)屬性是干嘛的首先要知道什么是基準(zhǔn)線熟丸,如下圖
1.基準(zhǔn)點(diǎn)是baseline
2.ascent:是baseline之上至字符最高處的距離
3.descent:是baseline之下至字符最低處的距離
4.leading:是上一行字符的descent到下一行的ascent之間的距離,也就是相鄰行間的空白距離
5.top:是指的是最高字符到baseline的值,即ascent的最大值
6.bottom:是指最低字符到baseline的值,即descent的最大值
其實(shí)基準(zhǔn)線對于對自定義View有一定了解的小伙伴都會(huì)比較熟悉
所以設(shè)置基準(zhǔn)線對齊有什么區(qū)別呢?
上代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JSON" />
<Button
android:layout_width="wrap_content"
android:layout_height="100dp"
android:text="Hello World" />
</LinearLayout>
分別設(shè)置android:baselineAligned 為true 與false
運(yùn)行如圖
2)基準(zhǔn)線對齊對象
<p>
xml屬性 : android:baselineAlignedChildIndex;
設(shè)置方法 : setBaselineAlignedChildIndex(int i);
作用 : 設(shè)置文字基線對齊的子控件;
基準(zhǔn)線對其上面有介紹過伪节,那么這個(gè)基準(zhǔn)線對齊對象其實(shí)就是設(shè)置文字基線對齊的子控件
接下來直接上代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#1E9066"
android:layout_weight="1"
android:baselineAlignedChildIndex="0"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="zero"
android:textSize="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One"
android:textSize="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TWo"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#CFCFCF"
android:layout_weight="1"
android:baselineAlignedChildIndex="1"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="zero"
android:textSize="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One"
android:textSize="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TWO"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#1E90FF"
android:layout_weight="1"
android:baselineAlignedChildIndex="2"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="zero"
android:textSize="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One"
android:textSize="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TWo"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
三個(gè)相同的布局分別設(shè)置基準(zhǔn)線為第一個(gè)光羞,第二個(gè)和第三個(gè)控件
運(yùn)行如圖
3)設(shè)分隔條
<p>
xml屬性 : android:divider="@drawable/shape"
?????android:showDividers="middle|beginning|end"
設(shè)置方法 : setDividerDrawable(Drawable);
?????setShowDividers(int showDividers)
作用 : 設(shè)置布局中兩個(gè)按鈕之間的分隔條;
分割線如果是圖片那就直接使用圖片就行,如果要使用顏色就必須使用shape來顯示怀大,直接使用顏色或Color是沒有用的 使用shape的時(shí)候要注意設(shè)置size屬性不設(shè)置寬高分割線就不會(huì)顯示出來纱兑,如果使用line那填充顏色只能使用stroke來顯示顏色
space_divider.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorAccent" />
<size android:height="1px" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:divider="@drawable/space_divider"
android:showDividers="middle"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="JSON" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello World" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello " />
</LinearLayout>
運(yùn)行如下
4)權(quán)重最小尺寸
<p>
xml屬性 : android:measureWithLargestChild;
設(shè)置方法 : setMeasureWithLargestChildEnable(boolean b);
作用 : 該屬性為true的時(shí)候, 所有帶權(quán)重的子元素都會(huì)具有最大子元素的最小尺寸;
代碼如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:measureWithLargestChild="true"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="JSON" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello Wor" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="He " />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="JSON" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello World" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello " />
</LinearLayout>
</LinearLayout>
運(yùn)行如圖
可以看出設(shè)置與沒有設(shè)置的區(qū)別還是很大的,當(dāng)我們設(shè)置權(quán)重最小尺寸時(shí),系統(tǒng)會(huì)把最大控件的最小尺寸作為其他子控件的尺寸化借,所以看到圖中上半部分的控件的大小其實(shí)都是一樣的
5)設(shè)置權(quán)重總和
<p>
xml屬性 : android:weightSum;
設(shè)置方法 : setWeightSum(float weightSum);
作用 : 設(shè)置權(quán)重的總和潜慎。(默認(rèn)是全部子控件權(quán)重之和);
定義weight總和的最大值。如果未指定該值蓖康,以所有子視圖的layout_weight屬性的累加值作為總和的最大值铐炫。
這個(gè)權(quán)重總和可能大家覺得并不是很有用,但是對于有些場景很有用蒜焊,比如我們需要一個(gè)布局在總布局的3/4的位置
如下所示
如果不使用權(quán)重總和這個(gè)屬性的話倒信,實(shí)現(xiàn)起來就只能用動(dòng)態(tài)布局通過屏幕的尺寸來重新布局子View了
而使用權(quán)重總和就可以很優(yōu)雅的解決這個(gè)問題了
代碼如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/colorAccent"
android:layout_weight="0.75"
android:measureWithLargestChild="true"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="JSON" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello Wor" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="He " />
</LinearLayout>
</LinearLayout>
這里其實(shí)就是設(shè)置父Layout的權(quán)重總和為1谷炸,子layout的權(quán)重為3/4
運(yùn)行如下
6)對齊方式(控制內(nèi)部子元素)
<p>
xml屬性 : android:gravity;
設(shè)置方法 : setGravity(int);
作用 : 設(shè)置布局管理器內(nèi)組件(子元素)的對齊方式,
支持的屬性 :
top, bottom, left, right,
center_vertical(垂直方向居中), center_horizontal(水平方向居中),
fill_vertical(垂直方向拉伸), fill_horizontal(水平方向拉伸),
center, fill,
clip_vertical, clip_horizontal;
可以同時(shí)指定多種對齊方式 : 如 left|center_vertical 左側(cè)垂直居中;
關(guān)于對齊方式就不做詳細(xì)的介紹了绍昂,因?yàn)槲蚁嘈糯蠹叶己芰私?/p>
7)排列方式
<p>
xml屬性 : android:orientation;
設(shè)置方法 : setOrientation(int i);
作用 : 設(shè)置布局管理器內(nèi)組件排列方式, 設(shè)置為horizontal(水平),vertical(垂直), 默認(rèn)為垂直排列;
這個(gè)大家應(yīng)該更了解蛉腌,也不做介紹了
寫在后面的幾句話
<p>
本來想把LinearLayout的源碼在一起進(jìn)行分析肉拓,但是考慮到篇幅確實(shí)夠長,所以分為兩篇進(jìn)行說明竞穷,下一篇會(huì)對LinearLayout的源碼進(jìn)行分析唐责。。瘾带。鼠哥。