Android 佈局LinearLayout——屬性baselineAligned的作用及baseline

  相信大家對LinearLayout已經相當熟悉,但你們是否瞭解它的屬性baselineAligned呢?

    Android官方文檔是這麼描述的:

     

     那到底這個屬性是做什麼用的呢?

         baselineAligned:基準線對齊。

     首先要解釋什麼是基準線,這個在中文中不常見,但在以字母爲書寫語言的其他國家非常常見。

     

      如上圖所示,紅線就是基線(baseline),是不是很熟悉,這不就是我們經常寫英文的四條線中的第三條嗎。

     

     那baselineAligned是做什麼用的呢?根據官方文檔,baselineAligned默認設置爲true,當設置爲false時,

      佈局文件和它的子組件的基準線不對齊。

      讓我們通過一個例子來看看效果怎樣的。

     

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content"
 5     android:baselineAligned="false"
 6     android:orientation="horizontal">
 7 
 8     <TextView
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_marginRight="5dip"
12         android:text="TextView:p" />
13     <TextView 
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:layout_marginRight="5dip"
17         android:textSize="30sp"
18         android:text="LargeTextView:p"
19         />
20 </LinearLayout>

 這是將baselineAligned值設置爲false時,也就是不對齊。看看運行效果:

  

  把baselineAligned值改爲true。

  

  看,差別明顯,這樣就很好的理解了baselineAligned的作用了。其實,這個並不難,但我覺得不管難不難,只要不明白,就應該多思考,想想爲什麼。

發佈了58 篇原創文章 · 獲贊 26 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章