谷动谷力

 找回密码
 立即注册
查看: 1308|回复: 0
打印 上一主题 下一主题
收起左侧

Android布局居中的几种做法

[复制链接]
跳转到指定楼层
楼主
发表于 2022-1-8 22:13:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Android布局居中的几种做法作者:且行且吟  
这篇文章主要介绍了Android布局居中的几种做法的相关资料,需要的朋友可以参考下

Android的布局文件中,如果想让一个组件(布局或View)居中显示在另一个布局(组件)中,可以由这么几种做法:

  • android:layout_gravity
  • android:gravity
  • android:layout_centerInParent

layout_gravity

android:layout_gravity ,用来指定当前组件(布局或View)在父组件(布局)中的位置,父布局应该是LinearLayout或者它的后裔。

layout_gravity取值可能是:

  • top
  • bottom
  • left
  • right
  • center_vertical
  • fill_vertical
  • center_horizontal
  • fill_horizontal
  • center
  • fill
  • clip_vertical
  • clip_horizontal
  • start
  • end

与居中相关的已经粗体标注出来。各种取值的具体含义,参看:

https://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html 。

gravity

android:gravity 是View的属性,用来指定View的子组件在View中的位置。适用于所有的View和布局。它的取值有很多,具体参见: https://developer.android.com/reference/android/view/Gravity.html

与居中相关的取值:

  • center
  • center_horizontal
  • center_vertical

当你设定一个布局 android:gravity="center" 时,它的子组件就会居中。当你设定一个View android:gravity="center" 时,它的内容会居中,以TextView为例,文字会居中。

layout_centerInParent

android:layout_centerInParent 是RelativeLayout的布局属性,如果一个组件(布局或View)的父布局是RelativeLayout,就可以使用这个属性来居中。其取值为 true 或 false 。

与其类似的还有:

  • android:layout_centerHorizontal
  • android:layout_centerVertical

具体含义和用法参看: https://developer.android.com/re ... t.LayoutParams.html

以上就是对Android 布局居中的几种方法整理,后续继续补充相关资料,谢谢大家对本站的支持!



+10
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|深圳市光明谷科技有限公司|光明谷商城|Sunshine Silicon Corpporation ( 粤ICP备14060730号|Sitemap

GMT+8, 2024-6-2 19:26 , Processed in 0.077472 second(s), 39 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表