打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android button 居中 - 代码分享 - 开源中国社区
userphoto

2010.11.24

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

代码片段(3)

[代码] 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">
08     </TextView>
09     <Gallery Android:id="@+id/gallery"
10         Android:layout_width="fill_parent"
11         Android:layout_height="wrap_content">
12     </Gallery>
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="返回主界面"/>
21 </RelativeLayout>

[图片] 运行结果

[代码] 简单说明

1 可以看到Button 与Gallery的对齐方式是居中对齐,也即Button 与Parent居中对齐。
2 另外,
3 Android:gravity="CENTER_VERTICAL“:这个是垂直居中对齐
4 Android:gravity="BOTTOM”:放在容器的底部
5 Android:gravity="CENTER“ :放在容器的中心
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Android笔记:Button居中|水平居中|垂直居中(总结)
Android 线性布局属性详解
关于修改状态栏的教程,表达不好还望海涵
Android高效率编码-细节,控件,架包,功能,工具,开源汇总
Android之布局
LinearLayout(线性布局)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服