android页面布局示例,实例001:使用线性布局(LinearLayout)来布局屏幕

第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所示。

9065539a0c0f512014cf41e53ca4a8a6.png

图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",代码如下所示。<?xmlversion ="1.0"encoding="utf-8"?>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="第一个按钮"

android:layout_weight="1"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="第二个按钮"

android:layout_weight="1"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="第三个按钮"

android:layout_weight="1"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="第四个按钮"

android:layout_weight="1"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="第五个按钮"

android:layout_weight="1"

/>

在上述代码中,在根LinearLayout视图组(ViewGroup)中包含了5个按钮(Button),它的子元素是以线性方式水平布局的。上述代码的运行效果如图1-2所示。

a8579eff0bd840d4f0e11955bba57085.png

图1-2  LinearLayout布局效果

【责任编辑:book TEL:(010)68476606】

点赞 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值