android:gravity定义当前元素内容或当前元素里所包含的子元素(下属元素)的显示位置。
android:layout_gravity定义当前元素在父元素里的显示位置。
例子1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="Text-01"
android:textColor="@android:color/white"
android:textSize="50sp"
android:layout_marginTop="20dip"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:text="Text-02"
android:textColor="@android:color/white"
android:textSize="50sp"
android:layout_marginTop="20dip"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0000ff"
android:text="Text-03"
android:textColor="@android:color/white"
android:textSize="50sp"
android:layout_marginTop="20dip"
android:layout_gravity="center"
/>
</LinearLayout>
显示效果:
TextView内容默认对齐方式是“左对齐”,如红色背景的TextView;
设置android:gravity="center"属性后,TextView内容变成居中显示,如绿色背景的TextView;
设置android:layout_gravity="center"属性改变不了TextView内容对齐方式,如蓝色背景的TextView。
例子2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dip"
android:background="#00ff00"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="#ff0000"
android:text="Text-01"
android:textColor="@android:color/white"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="#ff0000"
android:text="Text-02"
android:textColor="@android:color/white"
android:textSize="20sp" />
</LinearLayout>
<!-- android:gravity="center"表示LinearLayout
包含的所有子元素(2个TextView)在LinearLayout范围内居中显示 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dip"
android:background="#00ff00"
android:orientation="vertical"
android:layout_marginTop="10dip"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="#ff0000"
android:text="Text-03"
android:textColor="@android:color/white"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="#ff0000"
android:text="Text-04"
android:textColor="@android:color/white"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dip"
android:background="#00ff00"
android:orientation="vertical"
android:layout_marginTop="10dip"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="#ff0000"
android:text="Text-05"
android:textColor="@android:color/white"
android:textSize="20sp" />
<!-- android:layout_gravity="center_horizontal"表示当前TextView
在父元素(LinearLayout)里水平居中显示 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="#ff0000"
android:text="Text-06"
android:textColor="@android:color/white"
android:textSize="20sp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
显示效果:
第二组LinearLayout设置了android:gravity="center"属性后,其下属的2个TextView在父元素(LinearLayout)里居中显示;
第三组LinearLayout里的第二个TextView(Text-06)设置了android:layout_gravity="center_horizontal"属性后,该TextView在其父元素(LinearLayout)里水平居中显示。