在Android开发中UI设计十分重要,当用户使用一个软件时,最先感受到的不是这款软件的功能是否强大,而是界面设计是否精致,用户体验是否良好。也可以这样说,有一个好的界面设计去吸引用户的使用,才能让更多的用户体验到软件功能的强大。下面着重讲一下Android中几种常用布局的使用:

首先,需要说明的是,各个布局既可以单独使用,也可以嵌套使用,读者在实际应用中应灵活掌握。

1 LinearLayout 是一种Android中最常用的布局之一,它将自己包含的子元素按照一个方向排列。方向的设置通过Android:orientation=”vertical”(竖直)或者Android:orientation=”horizontal”(水平)来实现。


代码如下:

        
  1. <LinearLayoutandroid:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">
  2. <Buttonandroid:id="@+id/bt_1"android:layout_width="fill_parent"
  3. android:layout_height="wrap_content"android:text="用来验证RelativeLayout"
  4. />
  5. <Buttonandroid:id="@+id/bt_2"android:layout_width="fill_parent"
  6. android:layout_height="wrap_content"android:text="用来验证TableLayout"
  7. />
  8. <Buttonandroid:id="@+id/bt_3"android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"android:text="用来验证ListView"
  10. />
  11. <Buttonandroid:id="@+id/bt_4"android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"android:text="用来验证FrameLayout"
  13. />
  14. </LinearLayout>


2 FrameLayout对象好比一块在屏幕上提前预定好的空白区域,可以将一些元素填充在里面,如图片。所有元素都被放置在FrameLayout区域的最左上区域,而且无法为这些元素制指定一个确切的位置,若有多个元素,那么后面的元素会重叠显示在前一个元素上。

代码如下:

        
  1. <FrameLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"android:layout_height="match_parent">
  4. <ImageViewandroid:id="@+id/photo"
  5. android:src="@drawable/img"android:layout_width="wrap_content"
  6. android:layout_height="wrap_content"/>
  7. </FrameLayout>


3 RelativeLayout是一种相对布局,控件的位置是按照相对位置来计算的,后一个控件在什么位置依赖于前一个控件的基本位置。是布局最常用,也是最灵活的一种布局。

        
  1. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"android:layout_height="match_parent"
  3. android:padding="10dip">
  4. <TextViewandroid:id="@+id/label"android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"android:text="请输入用户名"/>
  6. <EditTextandroid:id="@+id/entry"android:layout_width="fill_parent"
  7. android:layout_height="wrap_content"android:layout_below="@id/label"/>
  8. <Buttonandroid:id="@+id/cancel"android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"android:layout_below="@id/entry"
  10. android:text="取消"/>
  11. </RelativeLayout>


4 TableLayout
TableLayout是指将子元素的位置分配到行或列中。Android的一个TableLayout有许多TableRow组成,每一个TableRow都会定义一个Row。TableLayout容器不会显示Row,Column,及Cell的边框线,每个Row拥有0个或多个Cell,每个Cell拥有一个View对象。
在使用tablelayout时,应注意每一个cell的宽度。

        
  1. <TableLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"android:layout_height="match_parent">
  4. <TableRow>
  5. <TextViewandroid:id="@+id/lable1"android:text="用户名"android:textStyle="bold"
  6. android:layout_width="55dip"android:gravity="center"/>
  7. <EditTextandroid:id="@+id/entry1"android:layout_width="250dip"
  8. android:layout_height="wrap_content"
  9. />
  10. </TableRow>
  11. <TableRow>
  12. <TextViewandroid:id="@+id/lable2"android:textStyle="bold"android:text="密码"
  13. android:layout_width="55dip"android:gravity="center"/>
  14. <EditTextandroid:id="@+id/entry2"
  15. android:layout_width="250dip"android:layout_height="wrap_content"
  16. android:password="true"android:scrollHorizontally="true"/>
  17. </TableRow>
  18. </TableLayout>

更多相关文章

  1. Android滑动菜单
  2. Android(安卓)实现布局动态加载
  3. 理解Android中dpi和分辨率的关系,谈谈Android做成适应全部手机的U
  4. Android中将xml布局文件转化为View树的过程分析(上)
  5. 教你使用Android(安卓)SDK布局优化工具layoutopt
  6. 【Android笔记 二】Location获取地理位置信息(上)
  7. Android(安卓)自定义UI
  8. Android_5.0定制--------keyCode实现SystemUI导航栏音量+ - 按钮
  9. android学习——使用TableLayout动态生成表格,并为tablerow中的列

随机推荐

  1. Android点滴记录
  2. 【Android(安卓)应用开发】GitHub 优秀的
  3. Android(安卓)APP增量升级及插件化实现方
  4. Android系统工具之Roblectric 使用过程中
  5. Android按返回键退出程序但不销毁
  6. Android运行报错:Error: Static interface
  7. 关于资源文件:颜色
  8. android中的广播接收器
  9. 解决Error:Android(安卓)Dex: com.androi
  10. android的软键盘的enter键的替换