Android实现button居中的方法
2015-09-24 12:28
786 查看
本文实例讲述了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:
<?xml version="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>
运行效果截图:
简单说明:
可以看到Button 与Gallery的对齐方式是居中对齐,也即Button 与Parent居中对齐。
另外,
Android:gravity="CENTER_VERTICAL":这个是垂直居中对齐
Android:gravity="BOTTOM":放在容器的底部
Android:gravity="CENTER" :放在容器的中心
希望本文所述对大家的Android程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析