欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android:layout_gravity和android:gravity的區(qū)別

 更新時間:2017年04月18日 11:51:21   作者:山代王  
本篇文章主要介紹了android中g(shù)iavity和layout_gravity的區(qū)別。具有很好的參考價值。下面跟著小編一起來看下吧

1.首先來看看Android:layout_gravity和android:gravity的使用區(qū)別。

android:gravity:

這個是針對控件里的元素來說的,用來控制元素在該控件里的顯示位置。例如,在一個Button按鈕控件中設(shè)置如下兩個屬性,

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

android:layout_gravity:

這個是針對控件本身而言,用來控制該控件在包含該控件的父控件中的位置。同樣,當(dāng)我們在Button按鈕控件中設(shè)置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 將對象放在其容器的左側(cè),不改變其大小.
right 將對象放在其容器的右側(cè),不改變其大小.
center_vertical 將對象縱向居中,不改變其大小. 
垂直對齊方式:垂直方向上居中對齊。
fill_vertical 必要的時候增加對象的縱向大小,以完全充滿其容器. 
垂直方向填充
center_horizontal 將對象橫向居中,不改變其大小. 
水平對齊方式:水平方向上居中對齊
fill_horizontal 必要的時候增加對象的橫向大小,以完全充滿其容器. 
水平方向填充
center 將對象橫縱居中,不改變其大小.
fill 必要的時候增加對象的橫縱向大小,以完全充滿其容器.
clip_vertical

附加選項,用于按照容器的邊來剪切對象的頂部和/或底部的內(nèi)容. 剪切基于其縱向?qū)R設(shè)置:頂部對齊時,剪切底部;底部對齊時剪切頂部;除此之外剪切頂部和底部.

垂直方向裁剪

clip_horizontal

附加選項,用于按照容器的邊來剪切對象的左側(cè)和/或右側(cè)的內(nèi)容. 剪切基于其橫向?qū)R設(shè)置:左側(cè)對齊時,剪切右側(cè);右側(cè)對齊時剪切左側(cè);除此之外剪切左側(cè)和右側(cè).

水平方向裁剪

我們主要來看看center_vertical和center_horizontal兩個屬性值,

center_vertical是指將對象在垂直方向上居中對齊,即在從上到下的方向上選擇中間的位置放好;center_horizontal是指將對象水平方向上居中對齊,即在從左到右的方向上選擇中間的位置放好。

3.特殊情況

當(dāng)我們采用LinearLayout布局時,有以下特殊情況需要我們注意:

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

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

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

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 > 
 <TextView 
 android:layout_width="100dip" 
 android:layout_height="100dip" 
 android:layout_gravity="bottom|center_horizontal" 
 android:gravity="center|bottom" 
 android:background="#00FF00" 
 android:text="@string/textview" 
 /> 
 
 <Button 
 android:layout_width="100dip" 
 android:layout_height="100dip" 
 android:layout_gravity="bottom|left" 
 android:gravity="left|top" 
 android:background="#FF0000" 
 android:text="@string/button" 
 /> 
</LinearLayout> 

其效果如圖: 

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

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

最新評論