你對LinearLayout到底有多少了解?(一)-屬性篇

寫在前面的話

<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)線

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 android:baselineAligned為true
圖3 android:baselineAligned為false
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)行如圖

圖4 baselineAlignedChildIndex
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)行如下

圖5 setShowDividers設(shè)置為middle
圖6 setShowDividers設(shè)置為end與beginning
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)行如圖

圖7 權(quán)重最小尺寸設(shè)置為true

可以看出設(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的位置

如下所示

圖8 例子圖

如果不使用權(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)行如下

圖9 權(quá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)行分析唐责。。瘾带。鼠哥。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市看政,隨后出現(xiàn)的幾起案子朴恳,更是在濱河造成了極大的恐慌,老刑警劉巖允蚣,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件于颖,死亡現(xiàn)場離奇詭異,居然都是意外死亡嚷兔,警方通過查閱死者的電腦和手機(jī)森渐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來冒晰,“玉大人同衣,你說我怎么就攤上這事『耍” “怎么了耐齐?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蒋情。 經(jīng)常有香客問我埠况,道長,這世上最難降的妖魔是什么棵癣? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任辕翰,我火速辦了婚禮,結(jié)果婚禮上浙巫,老公的妹妹穿的比我還像新娘金蜀。我一直安慰自己刷后,他們只是感情好的畴,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著尝胆,像睡著了一般丧裁。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上含衔,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天煎娇,我揣著相機(jī)與錄音二庵,去河邊找鬼。 笑死缓呛,一個(gè)胖子當(dāng)著我的面吹牛催享,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播哟绊,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼因妙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了票髓?” 一聲冷哼從身側(cè)響起攀涵,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎洽沟,沒想到半個(gè)月后以故,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡裆操,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年怒详,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片踪区。...
    茶點(diǎn)故事閱讀 39,779評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡棘利,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出朽缴,到底是詐尸還是另有隱情善玫,我是刑警寧澤,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布密强,位于F島的核電站茅郎,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏或渤。R本人自食惡果不足惜系冗,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望薪鹦。 院中可真熱鬧掌敬,春花似錦池磁、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至筛圆,卻和暖如春椿浓,著一層夾襖步出監(jiān)牢的瞬間轰绵,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工唧垦, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留振亮,地道東北人鞭莽。 一個(gè)月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓澎怒,卻偏偏與公主長得像,于是被迫代替她去往敵國和親星瘾。 傳聞我的和親對象是個(gè)殘疾皇子琳状,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,099評論 25 707
  • 歡迎Follow我的GitHub, 關(guān)注我的CSDN. 其余參考Android目錄. 轉(zhuǎn)載請注明出處:http:/...
    passiontim閱讀 4,762評論 0 31
  • Android功能強(qiáng)大边翁,界面華麗,但是眾多的布局屬性就害苦了開發(fā)者审洞,下面這篇文章結(jié)合了網(wǎng)上不少資料.第一類:屬性值...
    HangChen閱讀 4,866評論 0 24
  • 1芒澜、后悔自己年輕時(shí)不努力學(xué)習(xí)创淡,到老了一無所成琳彩,少狀不努力,老大徒傷悲碧浊。 2瘟仿、后悔自己年輕時(shí)選錯(cuò)了職業(yè)劳较。 3、后悔對...
    周榮_b3b8閱讀 2,932評論 0 0
  • 你說 你是煮不沸底一滴水 任柴薪怎地燥熱 烈火怎地熊熊 亦聚不成一塊冰 在九重寒冬天里相擁 偎暖 只因 歲月已然遠(yuǎn)...
    荒唐盛世閱讀 122評論 0 0