你對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, 就會阻止該布局管理器與其子元素的基準(zhǔn)線對齊;

要知道這個屬性是干嘛的首先要知道什么是基準(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有一定了解的小伙伴都會比較熟悉

所以設(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)線對其上面有介紹過,那么這個基準(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>

三個相同的布局分別設(shè)置基準(zhǔn)線為第一個,第二個和第三個控件

運(yùn)行如圖

圖4 baselineAlignedChildIndex
3)設(shè)分隔條

<p>
xml屬性 : android:divider="@drawable/shape"
?????android:showDividers="middle|beginning|end"
設(shè)置方法 : setDividerDrawable(Drawable);
?????setShowDividers(int showDividers)
作用 : 設(shè)置布局中兩個按鈕之間的分隔條;

分割線如果是圖片那就直接使用圖片就行,如果要使用顏色就必須使用shape來顯示,直接使用顏色或Color是沒有用的 使用shape的時(shí)候要注意設(shè)置size屬性不設(shè)置寬高分割線就不會顯示出來,如果使用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)重的子元素都會具有最大子元素的最小尺寸;

代碼如下

<?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)會把最大控件的最小尺寸作為其他子控件的尺寸,所以看到圖中上半部分的控件的大小其實(shí)都是一樣的

5)設(shè)置權(quán)重總和

<p>
xml屬性 : android:weightSum;
設(shè)置方法 : setWeightSum(float weightSum);
作用 : 設(shè)置權(quán)重的總和。(默認(rèn)是全部子控件權(quán)重之和);

定義weight總和的最大值。如果未指定該值,以所有子視圖的layout_weight屬性的累加值作為總和的最大值。

這個權(quán)重總和可能大家覺得并不是很有用,但是對于有些場景很有用,比如我們需要一個布局在總布局的3/4的位置

如下所示

圖8 例子圖

如果不使用權(quán)重總和這個屬性的話,實(shí)現(xiàn)起來就只能用動態(tài)布局通過屏幕的尺寸來重新布局子View了
而使用權(quán)重總和就可以很優(yōu)雅的解決這個問題了

代碼如下

<?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)為垂直排列;

這個大家應(yīng)該更了解,也不做介紹了

寫在后面的幾句話

<p>
本來想把LinearLayout的源碼在一起進(jìn)行分析,但是考慮到篇幅確實(shí)夠長,所以分為兩篇進(jìn)行說明,下一篇會對LinearLayout的源碼進(jìn)行分析。。。。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容