Android布局居中 Android布局居中的几种做法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android布局居中 Android布局居中的几种做法

且行且吟   2021-03-22 我要评论
想了解Android布局居中的几种做法的相关内容吗,且行且吟在本文为您仔细讲解Android布局居中的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,布局居中,Android,布局居中方法,Android,布局居中总结,下面大家一起来学习吧。

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

  1. android:layout_gravity
  2. android:gravity
  3. android:layout_centerInParent

layout_gravity

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

layout_gravity取值可能是:

  1. top
  2. bottom
  3. left
  4. right
  5. center_vertical
  6. fill_vertical
  7. center_horizontal
  8. fill_horizontal
  9. center
  10. fill
  11. clip_vertical
  12. clip_horizontal
  13. start
  14. 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 。

与居中相关的取值:

  1. center
  2. center_horizontal
  3. center_vertical

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

layout_centerInParent

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

与其类似的还有:

  1. android:layout_centerHorizontal
  2. android:layout_centerVertical

具体含义和用法参看: https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html 。

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们