Android 控件 - gravity 属性与 layout_gravity 属性

一、gravity 属性

1、基本介绍
  1. gravity 属性用于控制 View 内部的内容的对齐方式

  2. 作用对象是 View 内部的内容,例如,文本、子 View 等

常用值 说明
left 水平方向左对齐
right 水平方向右对齐
start 水平方向左对齐(RTL)
end 水平方向右对齐(RTL)
top 垂直方向顶部对齐
bottom 垂直方向底部对齐
center_horizontal 水平方向居中
center_vertical 垂直方向居中
center 水平和垂直方向都居中
2、演示
<LinearLayout
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:background="@color/black"
    android:gravity="center"
    android:orientation="vertical"
    android:text="content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:text="content" />
LinearLayout>

二、layout_gravity 属性

1、基本介绍
  1. layout_gravity 属性用于控制 View 在其父容器中的位置

  2. 作用对象是 View 本身,常用值与 gravity 属性相同

常用值 说明
left 水平方向左对齐
right 水平方向右对齐
start 水平方向左对齐(RTL)
end 水平方向右对齐(RTL)
top 垂直方向顶部对齐
bottom 垂直方向底部对齐
center_horizontal 水平方向居中
center_vertical 垂直方向居中
center 水平和垂直方向都居中
2、演示
<LinearLayout
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:background="@color/black"
    android:orientation="vertical"
    android:text="content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:layout_gravity="center"
        android:text="content" />
LinearLayout>

你可能感兴趣的:(Android,-,简化编程,android,java,java-ee,安卓,android-studio,开发语言,android,studio)