Android常用布局范例
在Android开发中UI设计十分重要,当用户使用一个软件时,最先感受到的不是这款软件的功能是否强大,而是界面设计是否精致,用户体验是否良好。也可以这样说,有一个好的界面设计去吸引用户的使用,才能让更多的用户体验到软件功能的强大。下面着重讲一下Android中几种常用布局的使用:
首先,需要说明的是,各个布局既可以单独使用,也可以嵌套使用,读者在实际应用中应灵活掌握。
1 LinearLayout 是一种Android中最常用的布局之一,它将自己包含的子元素按照一个方向排列。方向的设置通过Android:orientation=”vertical”(竖直)或者Android:orientation=”horizontal”(水平)来实现。
代码如下:
- <LinearLayoutandroid:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">
- <Buttonandroid:id="@+id/bt_1"android:layout_width="fill_parent"
- android:layout_height="wrap_content"android:text="用来验证RelativeLayout"
- />
- <Buttonandroid:id="@+id/bt_2"android:layout_width="fill_parent"
- android:layout_height="wrap_content"android:text="用来验证TableLayout"
- />
- <Buttonandroid:id="@+id/bt_3"android:layout_width="fill_parent"
- android:layout_height="wrap_content"android:text="用来验证ListView"
- />
- <Buttonandroid:id="@+id/bt_4"android:layout_width="fill_parent"
- android:layout_height="wrap_content"android:text="用来验证FrameLayout"
- />
- </LinearLayout>
2 FrameLayout对象好比一块在屏幕上提前预定好的空白区域,可以将一些元素填充在里面,如图片。所有元素都被放置在FrameLayout区域的最左上区域,而且无法为这些元素制指定一个确切的位置,若有多个元素,那么后面的元素会重叠显示在前一个元素上。
代码如下:
- <FrameLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"android:layout_height="match_parent">
- <ImageViewandroid:id="@+id/photo"
- android:src="@drawable/img"android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </FrameLayout>
3 RelativeLayout是一种相对布局,控件的位置是按照相对位置来计算的,后一个控件在什么位置依赖于前一个控件的基本位置。是布局最常用,也是最灵活的一种布局。
- <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"android:layout_height="match_parent"
- android:padding="10dip">
- <TextViewandroid:id="@+id/label"android:layout_width="fill_parent"
- android:layout_height="wrap_content"android:text="请输入用户名"/>
- <EditTextandroid:id="@+id/entry"android:layout_width="fill_parent"
- android:layout_height="wrap_content"android:layout_below="@id/label"/>
- <Buttonandroid:id="@+id/cancel"android:layout_width="wrap_content"
- android:layout_height="wrap_content"android:layout_below="@id/entry"
- android:text="取消"/>
- </RelativeLayout>
4 TableLayout
TableLayout是指将子元素的位置分配到行或列中。Android的一个TableLayout有许多TableRow组成,每一个TableRow都会定义一个Row。TableLayout容器不会显示Row,Column,及Cell的边框线,每个Row拥有0个或多个Cell,每个Cell拥有一个View对象。
在使用tablelayout时,应注意每一个cell的宽度。
- <TableLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"android:layout_height="match_parent">
- <TableRow>
- <TextViewandroid:id="@+id/lable1"android:text="用户名"android:textStyle="bold"
- android:layout_width="55dip"android:gravity="center"/>
- <EditTextandroid:id="@+id/entry1"android:layout_width="250dip"
- android:layout_height="wrap_content"
- />
- </TableRow>
- <TableRow>
- <TextViewandroid:id="@+id/lable2"android:textStyle="bold"android:text="密码"
- android:layout_width="55dip"android:gravity="center"/>
- <EditTextandroid:id="@+id/entry2"
- android:layout_width="250dip"android:layout_height="wrap_content"
- android:password="true"android:scrollHorizontally="true"/>
- </TableRow>
- </TableLayout>
更多相关文章
- Android滑动菜单
- Android(安卓)实现布局动态加载
- 理解Android中dpi和分辨率的关系,谈谈Android做成适应全部手机的U
- Android中将xml布局文件转化为View树的过程分析(上)
- 教你使用Android(安卓)SDK布局优化工具layoutopt
- 【Android笔记 二】Location获取地理位置信息(上)
- Android(安卓)自定义UI
- Android_5.0定制--------keyCode实现SystemUI导航栏音量+ - 按钮
- android学习——使用TableLayout动态生成表格,并为tablerow中的列