android:gravity和android:layout_gravity的區別,
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)裡水平置中顯示。