Android开发之layout布局+实例

来源:互联网 发布:vm12 mac os补丁 编辑:程序博客网 时间:2024/06/05 10:24
在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属性的值来决定是按行或者是按列逐个显示。

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

实现代码如下

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >     <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >         <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/username" />         <EditText            android:layout_width="fill_parent"            android:layout_height="wrap_content" />    </LinearLayout>     <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >         <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/userpass" />         <EditText            android:layout_width="fill_parent"            android:layout_height="wrap_content" />    </LinearLayout>     <TableLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:stretchColumns="*" >         <TableRow >             <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/login" />             <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/cancel" />        </TableRow>    </TableLayout> </LinearLayout>


相对布局中的视图组件是按相互之间的相对位置来确定的,并不是线性布局中的必须按行或按列单个显示。示例布局文件如下:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >     <Button        android:id="@+id/meiguiId"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="@string/meigui" />     <Button        android:id="@+id/meiId"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@id/meiguiId"        android:text="@string/mei" />     <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@id/meiguiId"        android:layout_alignParentRight="true"        android:text="@string/lan" />     <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/meiguiId"        android:text="@string/zhu" />     <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/meiguiId"        android:layout_alignParentRight="true"        android:text="@string/ju" /> </RelativeLayout>


显示效果如下:

表格布局的风格跟HTML中的表格比较接近,只是所采用的标签不同。

<TableLayout> 是顶级元素,说明采用的是表格布局

<TableRow>定义一个行

<TextView>定义一个单元格的内容

实现代码如下:

<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:stretchColumns="*" >     <TableRow >         <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/name2" />         <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/sex" />         <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/age" />         <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/tel" />    </TableRow>     <TableRow >         <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/namezs" />         <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/sexzs" />         <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/agezs" />         <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/telzs" />    </TableRow>     <TableRow >         <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/namely" />         <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/sexly" />         <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/agely" />         <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/telly" />    </TableRow> </TableLayout>


android:stretchColumns="*"

该属性指定每行都由第“0、1、2、3…”列占满空白空间。

gravity指定文字对齐方式,本例都设为居中对齐。

padding指定视图与视图内容间的空隙,单位为像素。

实现效果如下:

帧布局中的每一个组件都代表一个画面,这个程序的主要原理是大图片覆盖小的图片,首先由一张最小的图片显示,然后是比它大一点,在它外围增加点东西的这样显示的效果就像动态的似的。以此类推,然后再循环显示。

编写main.xml文件:

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center"    android:id="@+id/frame">  </FrameLayout>


在该布局文件中定义一个id为frame的帧布局文件。

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

主要实现代码如下:

package cn.class3g.activity; import android.app.Activity;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.FrameLayout; public class FrameLayoutActivity extends Activity {   FrameLayout frame = null;   private boolean flag = true;    // 由该类两个方法间的循环调用,实现界面不断更新。   class MyHandler extends Handler {       int i = 0;        public void handleMessage(Message msg) {          i++;          // 总共五幅图,依次显示          show(i % 5);          // 再次调用sleep方法          sleep(100);       }        public void sleep(long delayMillis) {          // 判断是否为真          if (flag) {              // 实质上是调用了一次handleMessage               sendMessageDelayed(obtainMessage(0), delayMillis);          }       }   }    // 该方法是被调用以更新帧布局的前景图片   void show(int j) {       // 获取五张图片       Drawable a = getResources().getDrawable(R.drawable.a1);       Drawable b = getResources().getDrawable(R.drawable.a2);       Drawable c = getResources().getDrawable(R.drawable.a3);       Drawable d = getResources().getDrawable(R.drawable.a4);       Drawable e = getResources().getDrawable(R.drawable.a5);       // 不同的情况,设置不同的前景       switch (j) {       case 0:          frame.setForeground(a);          break;       case 1:          frame.setForeground(b);          break;       case 2:          frame.setForeground(c);          break;       case 3:          frame.setForeground(d);          break;       case 4:          frame.setForeground(e);          break;       }   }    /** Called when the activity is first created. */   @Override   public void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.main);       frame = (FrameLayout) findViewById(R.id.frame);       // 创建一个Handler子类对象,要调用其方法       final MyHandler myHandler = new MyHandler();       myHandler.sleep(100);       // 为fram设置点击事件,当其被点击时,暂停。       frame.setOnClickListener(new View.OnClickListener() {          @Override          public void onClick(View v) {              flag = !flag;              myHandler.sleep(100);          }       });   }}


效果显示如下:

原创粉丝点击