Android筆記:Button居中|水平居中|垂直居中(總結)
阿新 • • 發佈:2019-02-13
- 鑑於各位前輩都有關於居中的示例,今天小弟在這結合自己的理解總結一下。
- 居中呢,這裡分兩種不同佈局方式的居中!分別是 LinearLayout 和Relative
Layout。
- 首先說的是LinearLayout佈局下的居中:直接貼原始碼
注意:android:layout_width="fill_parent" android:layout_height="fill_parent" 屬性中,若水平居中,至少在寬度上佔全屏;若垂直居中,則在高度上佔全屏
[html] view plaincopyprint?- <LinearLayout
- android:layout_width
- android:layout_height="fill_parent"
- android:gravity="center|center_horizontal|center_vertical">
- // 上面gravity屬性的引數:center為居中,center_horizontal為水平居中,center_vertical為垂直居中
- <Button
- android:id="@+id/Binding_button"
- android:layout_width="wrap_content"
- android:layout_height
- android:text="關聯新賬戶"/>
- </LinearLayout>
- 然後說的是Relative
Layout佈局下的居中:
- <RelativeLayoutxmlns:Android="http://schemas.android.com/apk/res/android"
- Android:layout_width="fill_parent"Android:layout_height="fill_parent">
- <ButtonAndroid:id
- Android:layout_height="wrap_content"Android:gravity="center_horizontal"
- Android:textSize="20sp"Android:layout_alignParentBottom="true"
- Android:layout_centerHorizontal="true"Android:text="返回主介面"/>
- </RelativeLayout>
簡單說明
Android:gravity="CENTER_VERTICAL“:這個是垂直居中對齊
Android:gravity="BOTTOM”:放在容器的底部
Android:gravity="CENTER“ :放在容器的中心
轉自:http://blog.csdn.net/mars2639/article/details/6870093