在main.xml 或者其他xml 布局文件中布局Button的时候,选择Android:gravity="center_horizontal",意思是Place object in the horizontal center of its container, not changing its size.我们用RelativeLayout 布局,这样可以使不同的组件有对齐的方式。
[代码] main.xml
01
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02
<
RelativeLayout
xmlns:Android
=
"http://schemas.android.com/apk/res/android"
03
Android:layout_width
=
"fill_parent"
04
Android:layout_height
=
"fill_parent"
>
05
<
TextView
Android:id
=
"@+id/gallerytext"
06
Android:layout_width
=
"fill_parent"
07
Android:layout_height
=
"wrap_content"
>
09
<
Gallery
Android:id
=
"@+id/gallery"
10
Android:layout_width
=
"fill_parent"
11
Android:layout_height
=
"wrap_content"
>
13
<
Button
Android:id
=
"@+id/btngal"
14
Android:layout_width
=
"wrap_content"
15
Android:layout_height
=
"wrap_content"
16
Android:gravity
=
"center_horizontal"
17
Android:textSize
=
"20sp"
18
Android:layout_alignParentBottom
=
"true"
19
Android:layout_centerHorizontal
=
"true"
20
Android:text
=
"返回主界面"
/>
[代码] 简单说明
1
可以看到Button 与Gallery的对齐方式是居中对齐,也即Button 与Parent居中对齐。
3
Android:gravity="CENTER_VERTICAL“:这个是垂直居中对齐
4
Android:gravity="BOTTOM”:放在容器的底部
5
Android:gravity="CENTER“ :放在容器的中心
转载于:https://www.cnblogs.com/thomaskwan/archive/2012/05/23/2515468.html