手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android实现button居中的方法
Android实现button居中的方法
摘要:本文实例讲述了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程序设计有所帮助。

【Android实现button居中的方法】相关文章:

android图像绘制(一)多种方法做图像镜像

Android中库项目的使用方法图文介绍

Android 获取进程内存使用情况方法

Android开发笔记之:复写按钮方法

Android绑定添加了注解的控件,及其事件方法

android系统在静音模式下关闭camera拍照声音的方法

android 实现圆角图片解决方案

Android自动禁用布局里的所有子控件

Android获取内外部存储器的容量

Android 将TabHost放在最下方显示

精品推荐
分类导航