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属性的值来决定是按行或者是按列逐个显示。

线性布局我们一般不会单用的,因为它太局限性了,它只能制作简单的界面,如果我们想做如下界面,那么就必须运用嵌套了。

实现代码如下

  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>  
  • 1
  • 2
  • 3
  • 4
  • 下一页

相关内容