- 时间:2022-12-08 21:12 编辑: 来源: 阅读:
- 扫一扫,手机访问
摘要:Android布局居中的几种做法
Android的布局文件中,如果想让一个组件(布局或View)居中显示在另一个布局(组件)中,可以由这么几种做法:
[list=1]
[*]android:layout_gravity[/*]
[*]android:gravity[/*]
[*]android:layout_centerInParent
[/*]
[/list]
[b]layout_gravity[/b]
android:layout_gravity ,用来指定当前组件(布局或View)在父组件(布局)中的位置,父布局应该是LinearLayout或者它的后裔。
layout_gravity取值可能是:
[list=1]
[*]top[/*]
[*]bottom[/*]
[*]left[/*]
[*]right[/*]
[*]center_vertical[/*]
[*]fill_vertical[/*]
[*]center_horizontal[/*]
[*]fill_horizontal[/*]
[*]center[/*]
[*]fill[/*]
[*]clip_vertical[/*]
[*]clip_horizontal[/*]
[*]start[/*]
[*]end
[/*]
[/list]
与居中相关的已经粗体标注出来。各种取值的具体含义,参看:
https://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html 。
[b]gravity[/b]
android:gravity 是View的属性,用来指定View的子组件在View中的位置。适用于所有的View和布局。它的取值有很多,具体参见: https://developer.android.com/reference/android/view/Gravity.html 。
与居中相关的取值:
[list=1]
[*]center[/*]
[*]center_horizontal[/*]
[*]center_vertical
[/*]
[/list]
当你设定一个布局 android:gravity="center" 时,它的子组件就会居中。当你设定一个View android:gravity="center" 时,它的内容会居中,以TextView为例,文字会居中。
[b]layout_centerInParent[/b]
android:layout_centerInParent 是RelativeLayout的布局属性,如果一个组件(布局或View)的父布局是RelativeLayout,就可以使用这个属性来居中。其取值为 true 或 false 。
与其类似的还有:
[list=1]
[*]android:layout_centerHorizontal[/*]
[*]android:layout_centerVertical
[/*]
[/list]
具体含义和用法参看: https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html 。
以上就是对Android 布局居中的几种方法整理,后续继续补充相关资料,谢谢大家对本站的支持!