Android雜談-RelativeLayout中的baseline是什么?


中文翻譯是基准線的意思,看似簡單又有些不好理解,其實這個baseline相當於筆記本里寫文字時候的底下的那條線,在RelativeLayout中baseline舉例:

比如,加入兩個相鄰的TextView,給第二個TextView一個大一點的padding(比如20dp),如果加了layout_alignBaseline到第二個TextView中的話,TextView中的文字"world"會與第一個TextView中的"hello"處於同一水平線,第二個TextView控件是上移了,就好像是"hello"和"world"處於同一條線上,如果沒有加layout_alignBaseline,具體看下面效果圖

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView 
        android:id="@+id/text1"
        android:text="hello"
        android:background="#aa0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />    
        <TextView
            android:text="world"
            android:background="#00ff00"
            android:padding="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/text1" android:layout_alignBaseline="@id/text1"
            />
</RelativeLayout>

 

1:未加layout_alignBaseline

2:加layout_alignBaseline,就像兩排文字hello world在一條線上寫的

3:測試垂直方面,將第二個TextView設置為layout_below屬性,未加layout_alignBaseline

4、測試垂直方面,將第二個TextView設置為layout_below屬性,加layout_alignBaseline。可以看到,它是將上面的控件覆蓋了,但是並沒有垂直居中

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM