android:layout_gravity和android:gravity的區別

來源:互聯網
上載者:User

標籤:android   blog   http   io   ar   使用   sp   on   div   

 androidlayoutbuttonencoding

1.首先來看看android:layout_gravity和android:gravity的使用區別。

android:gravity:

這個是針對控制項裡的元素來說的,用來控制元素在該控制項裡的顯示位置。例如,在一個Button按鈕控制項中設定如下兩個屬性,

android:gravity="left"和android:text="提交",這時Button上的文字“提交”將會位於Button的左部。

 

android:layout_gravity:

這個是針對控制項本身而言,用來控制該控制項在包含該控制項的父控制項中的位置。同樣,當我們在Button按鈕控制項中設定android:layout_gravity="left"屬性時,表示該Button按鈕將位於介面的左部。

 

2.屬性值:

 

這兩個屬性可選的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。

一個屬性可以包含多個值,需用“|”分開。其含義如下:

 

top 將對象放在其容器的頂部,不改變其大小.
bottom 將對象放在其容器的底部,不改變其大小.
left 將對象放在其容器的左側,不改變其大小.
right 將對象放在其容器的右側,不改變其大小.
center_vertical 將對象縱向置中,不改變其大小. 
垂直對齊:垂直方向上置中對齊。
fill_vertical 必要的時候增加對象的縱向大小,以完全充滿其容器. 
垂直方向填充
center_horizontal 將對象橫向置中,不改變其大小. 
水平對齊:水平方向上置中對齊
fill_horizontal 必要的時候增加對象的橫向大小,以完全充滿其容器. 
水平方向填充
center 將對象橫縱置中,不改變其大小.
fill 必要的時候增加對象的橫縱向大小,以完全充滿其容器.
clip_vertical

附加選項,用於按照容器的邊來剪下對象的頂部和/或底部的內容. 剪下基於其縱向對齊設定:頂部對齊時,剪下底部;底部對齊時剪下頂部;除此之外剪下頂部和底部.

垂直方向裁剪

clip_horizontal

附加選項,用於按照容器的邊來剪下對象的左側和/或右側的內容. 剪下基於其橫向對齊設定:左側對齊時,剪下右側;右側對齊時剪下左側;除此之外剪下左側和右側.

水平方向裁剪


我們主要來看看center_vertical和center_horizontal兩個屬性值,center_vertical是指將對象在垂直方向上置中對齊,即在從上到下的方向上選擇中間的位置放好;center_horizontal是指將對象水平方向上置中對齊,即在從左至右的方向上選擇中間的位置放好。

 

3.特殊情況

當我們採用LinearLayout布局時,有以下特殊情況需要我們注意:

 

(1)當 android:orientation="vertical"  時, android:layout_gravity只有水平方向的設定才起作用,垂直方向的設定不起作用。即:left,right,center_horizontal 是生效的。

(2)當 android:orientation="horizontal" 時, android:layout_gravity只有垂直方向的設定才起作用,水平方向的設定不起作用。即:top,bottom,center_vertical 是生效的。


下面以一個例子說明:(本例來源於:http://blog.csdn.net/dekunchenivan/article/details/6718678)

 

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <TextView    
  8.         android:layout_width="100dip"   
  9.         android:layout_height="100dip"  
  10.         android:layout_gravity="bottom|center_horizontal"  
  11.         android:gravity="center|bottom"  
  12.         android:background="#00FF00"  
  13.         android:text="@string/textview"  
  14.         />  
  15.   
  16.     <Button  
  17.         android:layout_width="100dip"  
  18.         android:layout_height="100dip"  
  19.         android:layout_gravity="bottom|left"  
  20.         android:gravity="left|top"  
  21.         android:background="#FF0000"  
  22.         android:text="@string/button"  
  23.         />  
  24. </LinearLayout>  

 

其效果  

 

 

 

在TextView中,我們設定了android:layout_gravity="bottom|center_horizontal"  ,但該TextView並沒有顯示在螢幕的下方正中央,表明只有center_horizontal屬性起了作用,這正是因為我們使用了LinearLayout布局,並且其android:orientation="vertical",只有水平方向的設定才會起作用,其他方向則會失效。同樣,Button也一樣。

 

android:layout_gravity和android:gravity的區別

聯繫我們

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