第1章 UI布局开发实例集锦
外观向来是工业产品的设计核心,是激发用户购买欲望的主要因素之一。本章讲的UI布局指的是手机界面布局,一款手机的屏幕界面效果是吸引用户购买的重要元素之一,因为消费者更倾向于选择界面美观的产品。在设计优美的界面之前,一定要先对屏幕进行布局。本章将使用具体实例的实现过程介绍在Android系统中规划UI界面的方法。
实例001:使用线性布局(LinearLayout)来布局屏幕
源码路径:daima\001
知识点介绍
在Android布局中,需要了解视图容器组件-ViewGroup的概念,使用视图容器组件ViewGroup的语法格式如下:
- ndroid.view.Viewgroup
ViewGroup的功能是包含并管理下级系列的Views和其他ViewGroup,是一个布局的基类。类ViewGroup好像一个View容器,负责对添加进来的View进行布局处理。一个ViewGroup可以添加到另一个ViewGroup中去。这是因为ViewGroup也继承于View.Viewgroup类,是其他容器类的基类。它们之间的关系如图1-1所示。
|
图1-1 各个类的继承关系 |
我们知道,一个Android程序是由一个或多个Activity组成的,每个Activity是一个UI容器,Activity本身不在用户界面中显示出来。在Android中,类View起了一个非常重要的作用,View是一个最基本的UI类,几乎所有的UI组件都是继承于View而实现的。
使用View的语法格式如下所示。
- android.view.View
线性布局即LinearLayout布局,是Android屏幕中常用的布局方式之一,功能是垂直地或水平地显示ViewGroup的子视图(View)元素。
具体实现
使用Eclipse创建一个名为"001"的Android工程。
编写布局文件"res/layour/main.xml",代码如下所示。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal">
- <Button android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="第一个按钮"
- android:layout_weight="1"
- />
- <Button android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="第二个按钮"
- android:layout_weight="1"
- />
- <Button android:id="@+id/button3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="第三个按钮"
- android:layout_weight="1"
- />
- <Button android:id="@+id/button4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="第四个按钮"
- android:layout_weight="1"
- />
- <Button android:id="@+id/button5"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="第五个按钮"
- android:layout_weight="1"
- />
- </LinearLayout>
在上述代码中,在根LinearLayout视图组(ViewGroup)中包含了5个按钮(Button),它的子元素是以线性方式水平布局的。上述代码的运行效果如图1-2所示。
本文出自:亿恩科技【www.enkj.com】
服务器租用/服务器托管中国五强!虚拟主机域名注册顶级提供商!15年品质保障!--亿恩科技[ENKJ.COM]
|