Android实现button居中的方法
Android实现button居中的方法
发布时间:2016-12-28 来源:查字典编辑
摘要:本文实例讲述了Android实现button居中的方法。分享给大家供大家参考。具体如下:通过在main.xml或者其他xml布局文件中布局B...

本文实例讲述了Android实现button居中的方法。分享给大家供大家参考。具体如下:

通过在main.xml 或者其他xml 布局文件中布局Button的时候,选择Android:gravity="center_horizontal",意思是Place object in the horizontal center of its container, not changing its size.我们用RelativeLayout 布局,这样可以使不同的组件有对齐的方式。

main.xml:

<"1.0" encoding="utf-8"?> <RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android" Android:layout_width="fill_parent" Android:layout_height="fill_parent"> <TextView Android:id="@+id/gallerytext" Android:layout_width="fill_parent" Android:layout_height="wrap_content"> </TextView> <Gallery Android:id="@+id/gallery" Android:layout_width="fill_parent" Android:layout_height="wrap_content"> </Gallery> <Button Android:id="@+id/btngal" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:gravity="center_horizontal" Android:textSize="20sp" Android:layout_alignParentBottom="true" Android:layout_centerHorizontal="true" Android:text="返回主界面"/> </RelativeLayout>

运行效果截图:

Android实现button居中的方法1

简单说明:

可以看到Button 与Gallery的对齐方式是居中对齐,也即Button 与Parent居中对齐。

另外,

Android:gravity="CENTER_VERTICAL":这个是垂直居中对齐

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

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

希望本文所述对大家的Android程序设计有所帮助。

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新安卓软件开发学习
热门安卓软件开发学习
编程开发子分类