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. }  


效果显示如下:

posted @ 2017-11-02 09:19  代码缔造的帝国  阅读(2350)  评论(0编辑  收藏  举报