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

寫在前面的話

<p>
LinearLayout對于開發(fā)來說,是使用最常用的布局控件之一,但是對于LinearLayout我們究竟有多了解呢?最近在看LinearLayout的源碼,看源碼過程中發(fā)現其實有很多東西自己并沒有使用到,對于LinearLayout的了解也是并沒有那么足,那么這篇文章就對LinearLayout進行更加詳細與深入的了解,從使用到源碼,一一進行解析!

一.LinearLayout屬性

<p>

1)基準線對齊

<p>
xml屬性 : android:baselineAligned;
設置方法 : setBaselineAligned(boolean b);
作用 : 如果該屬性為false, 就會阻止該布局管理器與其子元素的基準線對齊;

要知道這個屬性是干嘛的首先要知道什么是基準線,如下圖

圖1 基準線

1.基準點是baseline
2.ascent:是baseline之上至字符最高處的距離
3.descent:是baseline之下至字符最低處的距離
4.leading:是上一行字符的descent到下一行的ascent之間的距離,也就是相鄰行間的空白距離
5.top:是指的是最高字符到baseline的值,即ascent的最大值
6.bottom:是指最低字符到baseline的值,即descent的最大值

其實基準線對于對自定義View有一定了解的小伙伴都會比較熟悉

所以設置基準線對齊有什么區(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>

分別設置android:baselineAligned 為true 與false
運行如圖

圖2 android:baselineAligned為true
圖3 android:baselineAligned為false
2)基準線對齊對象

<p>
xml屬性 : android:baselineAlignedChildIndex;
設置方法 : setBaselineAlignedChildIndex(int i);
作用 : 設置文字基線對齊的子控件;

基準線對其上面有介紹過,那么這個基準線對齊對象其實就是設置文字基線對齊的子控件

接下來直接上代碼

<?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>

三個相同的布局分別設置基準線為第一個,第二個和第三個控件

運行如圖

圖4 baselineAlignedChildIndex
3)設分隔條

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

分割線如果是圖片那就直接使用圖片就行,如果要使用顏色就必須使用shape來顯示,直接使用顏色或Color是沒有用的 使用shape的時候要注意設置size屬性不設置寬高分割線就不會顯示出來,如果使用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>

運行如下

圖5 setShowDividers設置為middle
圖6 setShowDividers設置為end與beginning
4)權重最小尺寸

<p>
xml屬性 : android:measureWithLargestChild;
設置方法 : setMeasureWithLargestChildEnable(boolean b);
作用 : 該屬性為true的時候, 所有帶權重的子元素都會具有最大子元素的最小尺寸;

代碼如下

<?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>

運行如圖

圖7 權重最小尺寸設置為true

可以看出設置與沒有設置的區(qū)別還是很大的,當我們設置權重最小尺寸時,系統會把最大控件的最小尺寸作為其他子控件的尺寸,所以看到圖中上半部分的控件的大小其實都是一樣的

5)設置權重總和

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

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

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

如下所示

圖8 例子圖

如果不使用權重總和這個屬性的話,實現起來就只能用動態(tài)布局通過屏幕的尺寸來重新布局子View了
而使用權重總和就可以很優(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>

這里其實就是設置父Layout的權重總和為1,子layout的權重為3/4

運行如下

圖9 權重總和
6)對齊方式(控制內部子元素)

<p>
xml屬性 : android:gravity;
設置方法 : setGravity(int);
作用 : 設置布局管理器內組件(子元素)的對齊方式,

支持的屬性 :

  • top, bottom, left, right,

  • center_vertical(垂直方向居中), center_horizontal(水平方向居中),

  • fill_vertical(垂直方向拉伸), fill_horizontal(水平方向拉伸),

  • center, fill,

  • clip_vertical, clip_horizontal;

  • 可以同時指定多種對齊方式 : 如 left|center_vertical 左側垂直居中;

關于對齊方式就不做詳細的介紹了,因為我相信大家都很了解

7)排列方式

<p>
xml屬性 : android:orientation;
設置方法 : setOrientation(int i);
作用 : 設置布局管理器內組件排列方式, 設置為horizontal(水平),vertical(垂直), 默認為垂直排列;

這個大家應該更了解,也不做介紹了

寫在后面的幾句話

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容