Android:gravity屬性
線性布局常見的就是利用LinearLayout進行布局,其中有個比較重要的屬性就是android:gravity,在官方文檔中是這么描述這個屬性的:指定一個元素怎么放置它的內容,包括在X和Y軸,在它自己的邊框中。
下面我們將在一個簡單的TextView中應用android:gravity屬性。假設我們想要TextView內的內容在右側顯示,那么我們就可以編寫對應的XML布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/padding_medium" android:background="#ffffff" android:gravity="right" android:text="@string/hello_world" android:textColor="#ff0000" android:textSize="@dimen/font_size" /> </LinearLayout>
效果如下圖
盒模型
為了更加準確地控制TextView里面內容的位置,我們可以使用一系列的padding屬性來控制。在使用padding屬性之前,先科普一下padding和Marigin之間的區別,然后我們在通過實際的效果看看他們之間的差異。
下圖所示是一個類似盒子的模型,我們將通過下面的模型來講解Padding和Marigin之間的區別。從圖中可以看出,在Container(父控件)里面有一個子控件,假設是一個TextView控件。其中Margin是子控件與父控件之間的間隔大小。Border是子控件的邊框,它是子控件和父控件的邊界。Padding是指子控件中的內容(Content Area)與子控件Border的間隔大小。
margin屬性
Android中有一系列的margin屬性,下面讓我們看看其中的android:layout_marginRight屬性,為了有一個對比的效果,我們先將marginRight設為0dip,再將其設為50dip,如以下兩圖所示
android:layout_marginRight="0dip" |
android:layout_marginRight="50dip" |
從上圖中,我們可以看出,左圖TextView控件跟他的父控件的是沒有右間隔的,而右圖明顯的有一塊間隔(見右圖黃色圈圈部分)。
與marginRight相同的還有以下屬性,它們的原理都相同,就不一一細講了。
屬性名 | 相關方法 | 描述 |
android:layout_marginBottom | setMargins(int,int,int,int) | Specifies extra space on the bottom side of this view. |
android:layout_marginEnd | setMarginEnd(int) | Specifies extra space on the end side of this view. |
android:layout_marginLeft | setMargins(int,int,int,int) | Specifies extra space on the left side of this view. |
android:layout_marginRight | setMargins(int,int,int,int) | Specifies extra space on the right side of this view. |
android:layout_marginStart | setMarginStart(int) | Specifies extra space on the start side of this view. |
android:layout_marginTop | setMargins(int,int,int,int) | Specifies extra space on the top side of this view. |
padding屬性
下面讓我們來看看android:layout_paddingRight屬性。我們將在以下布局中,通過改變android:layout_paddingRight屬性,來看看變化。
為了有一個對比的效果,我們先將paddingRight設為0dip,再將其設為50dip,如以下兩圖所示
android:layout_paddingRight="0dip" | android:layout_paddingRight="50dip" |
![]() |
![]() |
從上圖中,我們可以看出,左圖TextView控件中的內容跟TextView的邊框(border)是沒有右間隔的,而右圖明顯的有一塊間隔(見右圖黃色圈圈部分)。
與paddingRight相同的還有以下屬性,它們的原理都相同,就不一一細講了。
屬性名 | 相關方法 | 描述 |
android:padding | setPaddingRelative(int,int,int,int) | Sets the padding, in pixels, of all four edges. |
android:paddingBottom | setPaddingRelative(int,int,int,int) | Sets the padding, in pixels, of the bottom edge; see padding . |
android:paddingEnd | setPaddingRelative(int,int,int,int) | Sets the padding, in pixels, of the end edge; see padding . |
android:paddingLeft | setPadding(int,int,int,int) | Sets the padding, in pixels, of the left edge; see padding . |
android:paddingRight | setPadding(int,int,int,int) | Sets the padding, in pixels, of the right edge; see padding . |
android:paddingStart | setPaddingRelative(int,int,int,int) | Sets the padding, in pixels, of the start edge; see padding . |
android:paddingTop | setPaddingRelative(int,int,int,int) | Sets the padding, in pixels, of the top edge; see padding . |
文章列表