Android佈局之LinearLayout

   LinearLayou(線性佈局佈局)html

一些重要的屬性:android

           一 orientation(朝向)   該屬性值有兩種一種是垂直朝向(verticle),還有一個是水平朝向(horizontal)app

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="horizontal"  
  3.     android:background="@drawable/blue"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="wrap_content">  
  6.   
  7.     <!-- view1 goes on top -->  
  8.     <TextView  
  9.         android:background="@drawable/box"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="@string/linear_layout_1_top"/>  
  13.   
  14.     <!-- view2 goes in the middle -->  
  15.     <TextView  
  16.         android:background="@drawable/box"  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:text="@string/linear_layout_1_middle"/>  
  20.   
  21.     <!-- view3 goes on the bottom -->  
  22.     <TextView  
  23.         android:background="@drawable/box"  
  24.         android:layout_width="match_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:text="@string/linear_layout_1_bottom"/>  
  27.   
  28. </LinearLayout>  

結果以下:

                     二 layout_weight(權重): ide

看下面一個例子: 該佈局填充整個屏幕,其中有三個字控件,分別佔據頭部,底部,中間佈局

在上一篇博客中咱們經過相對佈局也能夠實現(把高設置成0,height=0)spa

更多關於該屬性的細節能夠瀏覽http://hi.baidu.com/mendynew/item/39cd374192770bab60d7b915.net

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="vertical"  
  3.     android:background="@drawable/blue"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent">  
  6.   
  7.     <!-- view1 goes on top -->  
  8.     <TextView  
  9.         android:background="@drawable/box"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="@string/linear_layout_3_top"/>  
  13.   
  14.     <!-- view2 goes in the middle -->  
  15.     <TextView  
  16.         android:background="@drawable/box"  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_weight="1"  
  20.         android:text="@string/linear_layout_3_middle"/>  
  21.   
  22.     <!-- view3 goes on the bottom -->  
  23.     <TextView  
  24.         android:background="@drawable/box"  
  25.         android:layout_width="match_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:text="@string/linear_layout_3_bottom"/>  
  28.   
  29. </LinearLayout>  
運行結果:


下面一個例子,全部子空間的都是相同的寬度.也是經過該屬性來實現的.xml

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="horizontal"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <TextView  
  7.         android:background="@drawable/red"  
  8.         android:layout_width="0dip"  
  9.         android:layout_height="match_parent"  
  10.         android:layout_weight="1"/>  
  11.   
  12.     <TextView  
  13.         android:background="@drawable/green"  
  14.         android:layout_width="0dip"  
  15.         android:layout_height="match_parent"  
  16.         android:layout_weight="1"/>  
  17.   
  18.     <TextView  
  19.         android:background="@drawable/blue"  
  20.         android:layout_width="0dip"  
  21.         android:layout_height="match_parent"  
  22.         android:layout_weight="1"/>  
  23.   
  24.     <TextView  
  25.         android:background="@drawable/yellow"  
  26.         android:layout_width="0dip"  
  27.         android:layout_height="match_parent"  
  28.         android:layout_weight="1"/>  
  29.   
  30. </LinearLayout>  


下面看一個簡單表單的例子,htm

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="vertical"  
  3.     android:background="@drawable/blue"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="wrap_content"  
  6.     android:padding="10dip">  
  7.   
  8.     <TextView  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/linear_layout_5_instructions"/>  
  12.   
  13.     <EditText  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:background="@android :drawable/editbox_background"/>  
  17.   
  18.     <LinearLayout  
  19.         android:orientation="horizontal"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_gravity="right" >  
  23.   
  24.         <Button  
  25.             android:layout_width="wrap_content"  
  26.             android:layout_height="wrap_content"  
  27.             android:text="@string/linear_layout_5_cancel"/>  
  28.   
  29.         <Button  
  30.             android:layout_width="wrap_content"  
  31.             android:layout_height="wrap_content"  
  32.             android:layout_marginLeft="10dip"  
  33.             android:text="@string/linear_layout_5_ok" />  
  34.   
  35.     </LinearLayout>  
  36.   
  37. </LinearLayout>  


在上一篇博客中經過相對佈局也能佈局出這樣的,可是從效率上說,相對佈局要好不少,效率要高.從這個例子上看線性佈局的層級要深.blog

weight屬性還能夠實現以下佈局:

運行結果:

經過相對佈局也是能夠實現這樣的佈局,把button設置爲android:layout_alignParentBottom ="true"

                       三 gravity(重心)

下面來看一個例子:

[html]  view plain copy
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:padding="30dip" >  
  5.   
  6.     <LinearLayout  
  7.         android:id="@+id/layout"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent"  
  10.         android:background="@drawable/blue"  
  11.         android:orientation="vertical"  
  12.         android:padding="30dip" >  
  13.   
  14.         <TextView  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:background="@drawable/box"  
  18.             android:text="@string/linear_layout_8_c" />  
  19.   
  20.         <TextView  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content"  
  23.             android:background="@drawable/box"  
  24.             android:text="@string/linear_layout_8_b" />  
  25.   
  26.         <TextView  
  27.             android:layout_width="wrap_content"  
  28.             android:layout_height="wrap_content"  
  29.             android:background="@drawable/box"  
  30.             android:text="@string/linear_layout_8_c" />  
  31.     </LinearLayout>  
  32.   
  33. </FrameLayout>  

Java代碼:

[html]  view plain copy
  1. @Override  
  2.     public boolean onCreateOptionsMenu(Menu menu) {  
  3.         super.onCreateOptionsMenu(menu);  
  4.         menu.add(0, VERTICAL_ID, 0, R.string.linear_layout_8_vertical);  
  5.         menu.add(0, HORIZONTAL_ID, 0, R.string.linear_layout_8_horizontal);  
  6.         menu.add(0, TOP_ID, 0, R.string.linear_layout_8_top);  
  7.         menu.add(0, MIDDLE_ID, 0, R.string.linear_layout_8_middle);  
  8.         menu.add(0, BOTTOM_ID, 0, R.string.linear_layout_8_bottom);  
  9.         menu.add(0, LEFT_ID, 0, R.string.linear_layout_8_left);  
  10.         menu.add(0, CENTER_ID, 0, R.string.linear_layout_8_center);  
  11.         menu.add(0, RIGHT_ID, 0, R.string.linear_layout_8_right);  
  12.   
  13.         return true;  
  14.     }  
  15.   
  16.     @Override  
  17.     public boolean onOptionsItemSelected(MenuItem item) {  
  18.         switch (item.getItemId()) {  
  19.   
  20.         case VERTICAL_ID:  
  21.             mLinearLayout.setOrientation(LinearLayout.VERTICAL);  
  22.             return true;  
  23.         case HORIZONTAL_ID:  
  24.             mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);  
  25.             return true;  
  26.   
  27.         case TOP_ID:  
  28.             mLinearLayout.setVerticalGravity(Gravity.TOP);  
  29.             return true;  
  30.         case MIDDLE_ID:  
  31.             mLinearLayout.setVerticalGravity(Gravity.CENTER_VERTICAL);  
  32.             return true;  
  33.         case BOTTOM_ID:  
  34.             mLinearLayout.setVerticalGravity(Gravity.BOTTOM);  
  35.             return true;  
  36.   
  37.         case LEFT_ID:  
  38.             mLinearLayout.setHorizontalGravity(Gravity.LEFT);  
  39.             return true;  
  40.         case CENTER_ID:  
  41.             mLinearLayout.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);  
  42.             return true;  
  43.         case RIGHT_ID:  
  44.             mLinearLayout.setHorizontalGravity(Gravity.RIGHT);  
  45.             return true;  
  46.   
  47.         }  
  48.         return super.onOptionsItemSelected(item);  
  49.     }  
以上設置的gravity是經過Java代碼設置的,也能夠經過xml配置



須要注意的是layout_gravity和gravity的區別,前者是該控件相對於父控件的重心(gravity),後者該控件的子空間的重心(gravity)

相關文章
相關標籤/搜索