Android程序開發(fā)基礎之——頁面布局
1、gravity
gravity 這個英文單詞是重心的意思,在這里就表示停靠位置的意思。
android:gravity是對元素本身說的,元素本身的文本顯示在什么地方靠著換個屬性設置,不過不設置默認是在左側的。
android:layout_gravity是相對與它的父元素說的,說明元素顯示在父元素的什么位置。
比如說button: android:layout_gravity 表示按鈕在界面上的位置。 android:gravity表示button上的字在button上的位置。
可選值:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。
而且這些屬性是可以多選的,用“|”分開,默認這個的值是:Gravity.LEFT
2、layout_weight
layout_weight 用于給一個線性布局中的諸多視圖的重要度賦值。
所有的視圖都有一個layout_weight值,默認為零,意思是需要顯示多大的視圖就占據多大的屏幕空間。若賦一個高于零的值,則將父視圖中的可用空間分割,分割大小具體取決于每一個視圖的layout_weight 值以及該值在當前屏幕布局的整體 layout_weight值和在其它視圖屏幕布局的layout_weight值中所占的比率而定。
舉個例子:比如說我們在 水平方向上有一個文本標簽和兩個文本編輯元素。
該文本標簽并無指定layout_weight值,所以它將占據需要提供的最少空間。如果兩個文本編輯元素每一個的layout_weight值都設置為1,則兩者平分在父視圖布局剩余的寬度(因為我們聲明這兩者的重要度相等)。如果兩個文本編輯元素其中***個的layout_weight值設置為1,而第二個的設置為2,
則剩余空間的三分之二分給***個,三分之一分給第二個(數值越小,重要度越高)。
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg02"
android:orientation="horizontal" >
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4"
android:orientation="vertical" >
android:id="@+id/txtSpace1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
android:id="@+id/txtSpace2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mn_01" />
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mn_02" />
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mn_03" />
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mn_04" />
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4"
android:orientation="vertical" >
android:id="@+id/txtSpace3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
【編輯推薦】
- Android 的中長期焦慮?
- 風河公司:Android是目前最好的選擇
- droid碎片化嚴重:應用商購買400款手機測試