在談這個之前先啰嗦幾個概念。
基線:書寫英語單詞時為了規範書寫會設有四條線,從上至下第三條就是基線。基線對齊主要是為了兩個控件中顯示的英文單詞的基線對齊,如下所示:

start:在看api的時候經常會有start對齊,end對齊,start對齊主要是為了能夠在不同的textdirection(文本排列方向)的時候更好的對齊的一種方式,在textdirection為ltr(從左到右)時start對齊就等于左對齊,當textdirection為rtl(從右到左)時start對齊就等于右對齊。end同理。
通過gravity來實作view的文本對齊方式.先看下官方解釋。
android:gravity:specifies how to align the text by the view‘s x-
and/or y-axis when the text is smaller than the view.
must be one
or more (separated by ‘|‘) of the following constant values.
意思是說,當文本比view小的時候指定如何通過view的x、y軸線去對齊。這個屬性的值必須是下面給出的一個或多個,如果是多個則用‘|‘隔開。
可以用的屬性有:<code>top</code>、<code>bottom</code>、<code>left</code>、<code>right</code>、<code>center_vertical</code>、<code>fill_vertical</code>、<code>center_horizontal</code>、<code>fill_horizontal</code>、<code>center</code>、<code>fill</code>、<code>clip_vertical</code>、<code>clip_horizontal</code>、<code>start</code>、<code>end</code>
這些屬性中就談兩類,其他的類似,一個是上下左右這種基本類型的,還有一種是組合的,比如左下角,這是為:left|bottom。
左對齊:
左下角:
效果圖如下所示:
gravity:specifies how an object should position its
content, on both the x and y axes, within its own bounds.
must be one or more (separated by ‘|‘) of the following constant
values.
意思是,規定了一個對象以什麼樣的方式根據x、y軸去擺放自己的内容。這個屬性的值必須是下面給出的一個或多個,如果是多個則用‘|‘隔開。
可以用的屬性有:<code>top</code>、<code>bottom</code>、<code>left</code>、<code>right</code>、<code>center_vertical</code>、<code>fill_vertical</code>、<code>center_horizontal</code>、<code>fill_horizontal</code>、<code>center</code>、<code>fill</code>、<code>clip_vertical</code>、<code>clip_horizontal</code>、<code>start</code>、<code>end。</code>
同樣,這些屬性中就談兩類,其他的類似,一個是上下左右這種基本類型的,還有一種是組合的,比如左上角,這是為:left|bottom。
居右對齊:
居左下角對齊:
效果圖如下:
relativelayout主要用到相對位置的對齊,主要屬性有以下幾種
layout_above
該布局檔案的底部設定到指定id的對象的上方
layout_below
layout_alignbaseline
該布局檔案的基線與指定id的對象的基線對齊
layout_alignbottom
将該布局檔案的底部域指定的id的對象的底部對齊
layout_alignleft
layout_alignright
layout_aligntop
layout_alignstart
該布局檔案的開始(左邊)與指定id對象的開始(左邊)位置對齊
layout_alignend
該布局檔案的結束(右邊)與指定id對象的結束(右邊)位置對齊
layout_toleftof
該布局檔案的右邊放到指定id對象的左邊
類似:layout_tostartof
layout_torightof
該布局檔案的左邊放到指定id對象的右邊
layout_centerhorizontal
如果設定為true,則将該布局檔案中的自對象設定為水準居中
layout_centerinparent
layout_centervertical
layout_alignparentleft
如果設定為true,則将該布局檔案的左邊與其父對象的左邊對齊
layout_alignparentright
layout_alignparentstart
layout_alignparenttop
layout_alignparentend
layout_alignparentbottom
拿出兩個例子來簡單說明下,一個是将一個button放到另一個button右邊,另外一個是将一個button放到另一個button的右下角
右邊:
右下角:
後記:
這次談的内容比較粗,僅作抛磚引玉用。
原文連接配接:
demo下載下傳位址: