Android layout屬性之gravity和layout_gravity

來源:互聯網
上載者:User

標籤:android   http   io   ar   div   line   res   ef   

gravity用來描述當前view的內容在view中的位置。

例如    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:gravity="bottom"        android:text="hello" />那麼hello就會出現在按鈕的最下方。 如果當前view是個view group,例如<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="bottom" >     <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="hello" /> </LinearLayout>那按鈕會出現在layout的最下方。對這個例子,也就是螢幕的最下方。 如果把button的寬高都改為match_parent那對於    <Button        android:layout_width="match_parent"        android:layout_height="match_parent"        android:gravity="bottom"        android:text="hello" />button跟layout一樣大,hello是在該button的最下方。對於<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="bottom" >     <Button        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="hello" /> </LinearLayout>因為現在button和layouty一樣大,所以LinearLayout中的android:gravity沒有實際顯示效果。另外,該屬性無遞迴效果。 gravity是控制其內容或者包含的views在該view(或view group)中的位置。而layout_gravity是表示該view在其父容器view group中的位置。該屬性只在父容器是LinearLayout和FrameLayout時有效。如果要實現button出現在LinearLayout的最下方(上面第二個例子),用layout_gravity可以這麼寫<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="match_parent"     >     <Button        android:layout_gravity="bottom"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="hello" /> </LinearLayout> 只需要把LinearLayout中的android:gravity="bottom"刪掉,然後再Button加入android:layout_gravity="bottom" 如果把上面例子中的android:orientation=”horizontal"改為vertical,那麼button位置還是在上面。這是因為對於LinearLayout,如果是horizontal,則只能採用top,center,bottom等上下結構的屬性控制縱向位置;如果是vertical,則只能控制橫向位置。 與gravity一樣,如果該view和父容器view group的大小一樣,則該屬性無實際效果。事實上通過wrap_content和match_parent,這種情況經常出現。所以經常發現gravity和layout_gravity無效的情況。這時就要檢查是否是因為父子view大小相同了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.