1. 程式人生 > >Android筆記:Button居中|水平居中|垂直居中(總結)

Android筆記:Button居中|水平居中|垂直居中(總結)

-  鑑於各位前輩都有關於居中的示例,今天小弟在這結合自己的理解總結一下。

- 居中呢,這裡分兩種不同佈局方式的居中!分別是 LinearLayout 和RelativeLayout。

- 首先說的是LinearLayout佈局下的居中:直接貼原始碼

    注意:android:layout_width="fill_parent" android:layout_height="fill_parent" 屬性中,若水平居中,至少在寬度上佔全屏;若垂直居中,則在高度上佔全屏

[html] view plaincopyprint?
  1. <LinearLayout
  2.     android:layout_width
    ="fill_parent"
  3.     android:layout_height="fill_parent"
  4.     android:gravity="center|center_horizontal|center_vertical">
  5.     // 上面gravity屬性的引數:center為居中,center_horizontal為水平居中,center_vertical為垂直居中  
  6.     <Button
  7.         android:id="@+id/Binding_button"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height
    ="wrap_content"
  10.         android:text="關聯新賬戶"/>
  11. </LinearLayout>

- 然後說的是RelativeLayout佈局下的居中:

[html] view plaincopyprint?
  1. <RelativeLayoutxmlns:Android="http://schemas.android.com/apk/res/android"
  2.     Android:layout_width="fill_parent"Android:layout_height="fill_parent">
  3.     <ButtonAndroid:id
    ="@+id/btngal"Android:layout_width="wrap_content"
  4.         Android:layout_height="wrap_content"Android:gravity="center_horizontal"
  5.         Android:textSize="20sp"Android:layout_alignParentBottom="true"
  6.         Android:layout_centerHorizontal="true"Android:text="返回主介面"/>
  7. </RelativeLayout>

簡單說明

Android:gravity="CENTER_VERTICAL“:這個是垂直居中對齊

Android:gravity="BOTTOM”:放在容器的底部

Android:gravity="CENTER“ :放在容器的中心

轉自:http://blog.csdn.net/mars2639/article/details/6870093