粗談Android中的對齊

來源:互聯網
上載者:User

在談這個之前先囉嗦幾個概念。

基準:書寫英語單詞時為了規範書寫會設有四條線,從上至下第三條就是基準。基準對齊主要是為了兩個控制項中顯示的英文單詞的基準對齊,如下所示:

  

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軸線去對齊。這個屬性的值必須是下面給出的一個或多個,如果是多個則用'|'隔開。

  可以用的屬性有:topbottomleftrightcenter_verticalfill_verticalcenter_horizontalfill_horizontalcenterfillclip_verticalclip_horizontalstartend

  這些屬性中就談兩類,其他的類似,一個是上下左右這種基本類型的,還有一種是組合的,比如左下角,這是為:left|bottom。

  靠左對齊:

  

1 <TextView2         android:layout_width="100dip"3         android:layout_height="60dip"4         android:layout_marginTop="10dip"5         android:background="#ffff33"6         android:gravity="left"7         android:text="left" 8 />

  左下角:

1  <TextView2         android:layout_width="100dip"3         android:layout_height="60dip"4         android:layout_marginTop="10dip"5         android:background="#ffff33"6         android:gravity="left|bottom"7         android:text="leftbottom"8  />

  如下所示:

  

LinearLayout對齊

  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軸去擺放自己的內容。這個屬性的值必須是下面給出的一個或多個,如果是多個則用'|'隔開。

    可以用的屬性有:topbottomleftrightcenter_verticalfill_verticalcenter_horizontalfill_horizontalcenterfillclip_verticalclip_horizontalstartend。

    同樣,這些屬性中就談兩類,其他的類似,一個是上下左右這種基本類型的,還有一種是組合的,比如左上方,這是為:left|bottom。

   居靠右對齊:

  

 1 <LinearLayout 2         android:layout_width="match_parent" 3         android:background="#0000ff" 4         android:layout_height="0dip" 5         android:gravity="right" 6         android:layout_weight="1" > 7          8         <Button  9             android:layout_height="wrap_content"10             android:layout_width="wrap_content"11             android:text="right"12             />13         14 </LinearLayout>

  居左下角對齊:

 1 <LinearLayout 2         android:layout_width="match_parent" 3         android:background="#ffff33" 4         android:layout_height="0dip" 5         android:layout_marginTop="10dip" 6         android:gravity="left|bottom" 7         android:layout_weight="1" > 8          9          <Button 10             android:layout_height="wrap_content"11             android:layout_width="wrap_content"12             android:text="left_bottom"13             />14 </LinearLayout>

  如下:

  

RelativeLayout對象

  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的右下角

  右邊:

  

 1  <RelativeLayout 2         android:layout_width="match_parent" 3         android:layout_height="wrap_content" > 4  5         <Button 6             android:id="@+id/btnStand" 7             android:layout_width="wrap_content" 8             android:layout_height="wrap_content" 9             android:text="標準物1" />10 11         <Button12             android:layout_width="100dip"13             android:layout_height="wrap_content"14             android:layout_toRightOf="@id/btnStand"15             android:text="right->標準物1" />16  </RelativeLayout>

 

  右下角:

 1 <RelativeLayout 2         android:layout_width="match_parent" 3         android:layout_height="wrap_content" > 4  5         <Button 6             android:id="@+id/btnStand2" 7             android:layout_width="wrap_content" 8             android:layout_height="wrap_content" 9             android:text="標準物2" />10 11         <Button12             android:layout_width="100dip"13             android:layout_height="wrap_content"14             android:layout_below="@id/btnStand2"15             android:layout_toRightOf="@id/btnStand2"16             android:text="right_bottom->標準物2" />17 </RelativeLayout>

  如下所示:

 

後記:

  這次談的內容比較粗,僅作拋磚引玉用。

原文串連:http://www.cnblogs.com/luoaz/p/3719767.html

Demo:https://github.com/xiaoai-opensource/alignDemo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.