android:gravity和android:layout_gravity的區別,

來源:互聯網
上載者:User

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)裡水平置中顯示。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.