Android開發之layout布局+實例


在Android 應用中,用戶界面是非常重要的,它是人與手機之間傳遞、交換信息的媒介和對話接口,是Android 系統的重要組成部分。它實現信息的內部形式與用戶可以接受形式之間的轉換。iPhone 之所以被人們所推崇,除了其功能強大之外,最重要的是完美的UI(用戶界面)設計,在Android 系統中,我們也可以開發出與iPhone

同樣絢麗多彩的UI。

一個Android 應用的用戶界面是由View 和ViewGroup 對象構建的。它們有很多的種類,並且都是View 類的子類,View 類是Android 系統平台上用戶界面表示的基本單元。View類的一些子類被統稱為“widgets(工具)”,它們提供了諸如文本輸入框和按鈕之類的UI

對象的完整實現。ViewGroup 是View 的一個擴展,它可以容納多個子View。通過擴展ViewGroup 類,你可以創建由相互聯系的子View 組成的復合控件。ViewGroup 類同樣可以被擴展用作layout(布局)管理器,如LinearLayout(線性布局)、TableLayout(表格布局)

以及RelativeLayout(相對布局)等布局架構。並且用戶可以通過用戶界面與程序進行交互。

    首先我們來說說LinearLayout。

“LinearLayout”翻譯成中文是“線性布局”,所謂線性布局就是在該標簽下的所有子元素會根據其orientation屬性的值來決定是按行或者是按列逐個顯示。

    線性布局我們一般不會單用的,因為它太局限性了,它只能制作簡單的界面,如果我們想做如下界面,那么就必須運用嵌套了。

實現代碼如下

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5.     android:layout_width="match_parent"  
  6.   
  7.     android:layout_height="match_parent"  
  8.   
  9.     android:orientation="vertical" >  
  10.   
  11.    
  12.   
  13.     <LinearLayout  
  14.   
  15.         android:layout_width="match_parent"  
  16.   
  17.         android:layout_height="wrap_content"  
  18.   
  19.         android:orientation="horizontal" >  
  20.   
  21.    
  22.   
  23.         <TextView  
  24.   
  25.             android:layout_width="wrap_content"  
  26.   
  27.             android:layout_height="wrap_content"  
  28.   
  29.             android:text="@string/username" />  
  30.   
  31.    
  32.   
  33.         <EditText  
  34.   
  35.             android:layout_width="fill_parent"  
  36.   
  37.             android:layout_height="wrap_content" />  
  38.   
  39.     </LinearLayout>  
  40.   
  41.    
  42.   
  43.     <LinearLayout  
  44.   
  45.         android:layout_width="match_parent"  
  46.   
  47.         android:layout_height="wrap_content"  
  48.   
  49.         android:orientation="horizontal" >  
  50.   
  51.    
  52.   
  53.         <TextView  
  54.   
  55.             android:layout_width="wrap_content"  
  56.   
  57.             android:layout_height="wrap_content"  
  58.   
  59.             android:text="@string/userpass" />  
  60.   
  61.    
  62.   
  63.         <EditText  
  64.   
  65.             android:layout_width="fill_parent"  
  66.   
  67.             android:layout_height="wrap_content" />  
  68.   
  69.     </LinearLayout>  
  70.   
  71.    
  72.   
  73.     <TableLayout  
  74.   
  75.         android:layout_width="match_parent"  
  76.   
  77.         android:layout_height="match_parent"  
  78.   
  79.         android:stretchColumns="*" >  
  80.   
  81.    
  82.   
  83.         <TableRow >  
  84.   
  85.    
  86.   
  87.             <Button  
  88.   
  89.                 android:layout_width="wrap_content"  
  90.   
  91.                 android:layout_height="wrap_content"  
  92.   
  93.                 android:text="@string/login" />  
  94.   
  95.    
  96.   
  97.             <Button  
  98.   
  99.                 android:layout_width="wrap_content"  
  100.   
  101.                 android:layout_height="wrap_content"  
  102.   
  103.                 android:text="@string/cancel" />  
  104.   
  105.         </TableRow>  
  106.   
  107.     </TableLayout>  
  108.   
  109.    
  110.   
  111. </LinearLayout>  


相對布局中的視圖組件是按相互之間的相對位置來確定的,並不是線性布局中的必須按行或按列單個顯示。示例布局文件如下:

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5.     android:layout_width="match_parent"  
  6.   
  7.     android:layout_height="match_parent" >  
  8.   
  9.    
  10.   
  11.     <Button  
  12.   
  13.         android:id="@+id/meiguiId"  
  14.   
  15.         android:layout_width="wrap_content"  
  16.   
  17.         android:layout_height="wrap_content"  
  18.   
  19.         android:layout_centerInParent="true"  
  20.   
  21.         android:text="@string/meigui" />  
  22.   
  23.    
  24.   
  25.     <Button  
  26.   
  27.         android:id="@+id/meiId"  
  28.   
  29.         android:layout_width="wrap_content"  
  30.   
  31.         android:layout_height="wrap_content"  
  32.   
  33.         android:layout_above="@id/meiguiId"  
  34.   
  35.         android:text="@string/mei" />  
  36.   
  37.    
  38.   
  39.     <Button  
  40.   
  41.         android:layout_width="wrap_content"  
  42.   
  43.         android:layout_height="wrap_content"  
  44.   
  45.         android:layout_above="@id/meiguiId"  
  46.   
  47.         android:layout_alignParentRight="true"  
  48.   
  49.         android:text="@string/lan" />  
  50.   
  51.    
  52.   
  53.     <Button  
  54.   
  55.         android:layout_width="wrap_content"  
  56.   
  57.         android:layout_height="wrap_content"  
  58.   
  59.         android:layout_below="@id/meiguiId"  
  60.   
  61.         android:text="@string/zhu" />  
  62.   
  63.    
  64.   
  65.     <Button  
  66.   
  67.         android:layout_width="wrap_content"  
  68.   
  69.         android:layout_height="wrap_content"  
  70.   
  71.         android:layout_below="@id/meiguiId"  
  72.   
  73.         android:layout_alignParentRight="true"  
  74.   
  75.         android:text="@string/ju" />  
  76.   
  77.    
  78.   
  79. </RelativeLayout>  


顯示效果如下:

表格布局的風格跟HTML中的表格比較接近,只是所采用的標簽不同。

<TableLayout> 是頂級元素,說明采用的是表格布局

<TableRow>定義一個行

<TextView>定義一個單元格的內容

實現代碼如下:

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5.     android:layout_width="match_parent"  
  6.   
  7.     android:layout_height="match_parent"  
  8.   
  9.     android:stretchColumns="*" >  
  10.   
  11.    
  12.   
  13.     <TableRow >  
  14.   
  15.    
  16.   
  17.         <TextView  
  18.   
  19.             android:layout_width="wrap_content"  
  20.   
  21.             android:layout_height="wrap_content"  
  22.   
  23.             android:text="@string/name2" />  
  24.   
  25.    
  26.   
  27.         <TextView  
  28.   
  29.             android:layout_width="wrap_content"  
  30.   
  31.             android:layout_height="wrap_content"  
  32.   
  33.             android:text="@string/sex" />  
  34.   
  35.    
  36.   
  37.         <TextView  
  38.   
  39.             android:layout_width="wrap_content"  
  40.   
  41.             android:layout_height="wrap_content"  
  42.   
  43.             android:text="@string/age" />  
  44.   
  45.    
  46.   
  47.         <TextView  
  48.   
  49.             android:layout_width="wrap_content"  
  50.   
  51.             android:layout_height="wrap_content"  
  52.   
  53.             android:text="@string/tel" />  
  54.   
  55.     </TableRow>  
  56.   
  57.    
  58.   
  59.     <TableRow >  
  60.   
  61.    
  62.   
  63.         <TextView  
  64.   
  65.             android:layout_width="wrap_content"  
  66.   
  67.             android:layout_height="wrap_content"  
  68.   
  69.             android:text="@string/namezs" />  
  70.   
  71.    
  72.   
  73.         <TextView  
  74.   
  75.             android:layout_width="wrap_content"  
  76.   
  77.             android:layout_height="wrap_content"  
  78.   
  79.             android:text="@string/sexzs" />  
  80.   
  81.    
  82.   
  83.         <TextView  
  84.   
  85.             android:layout_width="wrap_content"  
  86.   
  87.             android:layout_height="wrap_content"  
  88.   
  89.             android:text="@string/agezs" />  
  90.   
  91.    
  92.   
  93.         <TextView  
  94.   
  95.             android:layout_width="wrap_content"  
  96.   
  97.             android:layout_height="wrap_content"  
  98.   
  99.             android:text="@string/telzs" />  
  100.   
  101.     </TableRow>  
  102.   
  103.    
  104.   
  105.     <TableRow >  
  106.   
  107.    
  108.   
  109.         <TextView  
  110.   
  111.             android:layout_width="wrap_content"  
  112.   
  113.             android:layout_height="wrap_content"  
  114.   
  115.             android:text="@string/namely" />  
  116.   
  117.    
  118.   
  119.         <TextView  
  120.   
  121.             android:layout_width="wrap_content"  
  122.   
  123.             android:layout_height="wrap_content"  
  124.   
  125.             android:text="@string/sexly" />  
  126.   
  127.    
  128.   
  129.         <TextView  
  130.   
  131.             android:layout_width="wrap_content"  
  132.   
  133.             android:layout_height="wrap_content"  
  134.   
  135.             android:text="@string/agely" />  
  136.   
  137.    
  138.   
  139.         <TextView  
  140.   
  141.             android:layout_width="wrap_content"  
  142.   
  143.             android:layout_height="wrap_content"  
  144.   
  145.             android:text="@string/telly" />  
  146.   
  147.     </TableRow>  
  148.   
  149.    
  150.   
  151. </TableLayout>  


android:stretchColumns="*"

該屬性指定每行都由第“0、1、2、3…”列占滿空白空間。

gravity指定文字對齊方式,本例都設為居中對齊。

padding指定視圖與視圖內容間的空隙,單位為像素。

實現效果如下:

幀布局中的每一個組件都代表一個畫面,這個程序的主要原理是大圖片覆蓋小的圖片,首先由一張最小的圖片顯示,然后是比它大一點,在它外圍增加點東西的這樣顯示的效果就像動態的似的。以此類推,然后再循環顯示。

編寫main.xml文件:

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5.     android:layout_width="wrap_content"  
  6.   
  7.     android:layout_height="wrap_content"  
  8.   
  9.     android:layout_gravity="center"  
  10.   
  11.     android:id="@+id/frame"  
  12.   
  13. >   
  14.   
  15.    
  16.   
  17. </FrameLayout>  


在該布局文件中定義一個id為frame的幀布局文件。

然后把我們需要的圖片放入工程resàdrawable下。

主要實現代碼如下:

[html]  view plain  copy
 
  1. package cn.class3g.activity;  
  2.   
  3.    
  4.   
  5. import android.app.Activity;  
  6.   
  7. import android.graphics.drawable.Drawable;  
  8.   
  9. import android.os.Bundle;  
  10.   
  11. import android.os.Handler;  
  12.   
  13. import android.os.Message;  
  14.   
  15. import android.view.View;  
  16.   
  17. import android.widget.FrameLayout;  
  18.   
  19.    
  20.   
  21. public class FrameLayoutActivity extends Activity {  
  22.   
  23.    FrameLayout frame = null;  
  24.   
  25.    private boolean flag = true;  
  26.   
  27.    
  28.   
  29.    // 由該類兩個方法間的循環調用,實現界面不斷更新。  
  30.   
  31.    class MyHandler extends Handler {  
  32.   
  33.        int i = 0;  
  34.   
  35.    
  36.   
  37.        public void handleMessage(Message msg) {  
  38.   
  39.           i++;  
  40.   
  41.           // 總共五幅圖,依次顯示  
  42.   
  43.           show(i % 5);  
  44.   
  45.           // 再次調用sleep方法  
  46.   
  47.           sleep(100);  
  48.   
  49.        }  
  50.   
  51.    
  52.   
  53.        public void sleep(long delayMillis) {  
  54.   
  55.           // 判斷是否為真  
  56.   
  57.           if (flag) {  
  58.   
  59.               // 實質上是調用了一次handleMessage  
  60.   
  61.    
  62.   
  63.               sendMessageDelayed(obtainMessage(0), delayMillis);  
  64.   
  65.           }  
  66.   
  67.        }  
  68.   
  69.    }  
  70.   
  71.    
  72.   
  73.    // 該方法是被調用以更新幀布局的前景圖片  
  74.   
  75.    void show(int j) {  
  76.   
  77.        // 獲取五張圖片  
  78.   
  79.        Drawable a = getResources().getDrawable(R.drawable.a1);  
  80.   
  81.        Drawable b = getResources().getDrawable(R.drawable.a2);  
  82.   
  83.        Drawable c = getResources().getDrawable(R.drawable.a3);  
  84.   
  85.        Drawable d = getResources().getDrawable(R.drawable.a4);  
  86.   
  87.        Drawable e = getResources().getDrawable(R.drawable.a5);  
  88.   
  89.        // 不同的情況,設置不同的前景  
  90.   
  91.        switch (j) {  
  92.   
  93.        case 0:  
  94.   
  95.           frame.setForeground(a);  
  96.   
  97.           break;  
  98.   
  99.        case 1:  
  100.   
  101.           frame.setForeground(b);  
  102.   
  103.           break;  
  104.   
  105.        case 2:  
  106.   
  107.           frame.setForeground(c);  
  108.   
  109.           break;  
  110.   
  111.        case 3:  
  112.   
  113.           frame.setForeground(d);  
  114.   
  115.           break;  
  116.   
  117.        case 4:  
  118.   
  119.           frame.setForeground(e);  
  120.   
  121.           break;  
  122.   
  123.        }  
  124.   
  125.    }  
  126.   
  127.    
  128.   
  129.    /** Called when the activity is first created. */  
  130.   
  131.    @Override  
  132.   
  133.    public void onCreate(Bundle savedInstanceState) {  
  134.   
  135.        super.onCreate(savedInstanceState);  
  136.   
  137.        setContentView(R.layout.main);  
  138.   
  139.        frame = (FrameLayout) findViewById(R.id.frame);  
  140.   
  141.        // 創建一個Handler子類對象,要調用其方法  
  142.   
  143.        final MyHandler myHandler = new MyHandler();  
  144.   
  145.        myHandler.sleep(100);  
  146.   
  147.        // 為fram設置點擊事件,當其被點擊時,暫停。  
  148.   
  149.        frame.setOnClickListener(new View.OnClickListener() {  
  150.   
  151.           @Override  
  152.   
  153.           public void onClick(View v) {  
  154.   
  155.               flag = !flag;  
  156.   
  157.               myHandler.sleep(100);  
  158.   
  159.           }  
  160.   
  161.        });  
  162.   
  163.    }  
  164.   
  165. }  


效果顯示如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM