Android的UI设计有好几种界面程序编写方式。大体上可分为两大类:一类是利用可视化工具来进行,允许你进行拖拽控件来进行布局;还有一类是编写xml文档来进行布局。这两种方法可以相互转换。

1、常见的控件的使用方法

  • TextView
  • Button:一般需要注册监听器来对点击按键的事件做出响应
  • EditText:允许用户在控件里输入和编辑内容,并可以在在程序中对这些内容进行处理。此外,可以使用android:hint属性来指定一段提示性的文本。
  • ImageView:展示图片的一个控件。通过android:src属性来指定图片的位置
  • ProcessBar:用于在界面显示一个进度条(用android:style属性可以设置为圆形或条状),并且可以通过android:visible属性来设置控件的可见性(三种:visible、invisible(控件不可见,但是仍然占用屏幕空间,可以理解为变为了透明状态)、gone(控件不可见,并且不再占用屏幕空间))。
  • AlertDialog:可以在当前界面弹出一个对话框,这个对话框是置顶于所有界面元素之上的,能够屏蔽掉其他控件的交互能力,因此AlertDialog一般用于提示一些非常重要的内容或警告信息,例如一些确认信息等。
  • ProgressDialog:和AlertDialog类似,都可以在界面弹出一个对话框,并且可以屏蔽掉其他界面的交互能力。不同的是,该控件会在对话框中显示一个进度条,一般用于表示比较耗时的当前操作,让用户耐心等待。

2、四种基本布局

LinearLayout

LinearLayout又称作线性布局,是一种非常常用的布局。该布局有一个专属属性android:orientation,这个属性有两个选择vertical和horizontal,代表着线性排列的规律(水平方向还是垂直方向)。

演示:左图是vertical,右图是horizonal

                  

  • 如果LinearLayout 的排列方向是 horizontal,内部的控件就绝对不能将宽度指定为match_parent,因为这样的话单独一个控件就会将整个水平方向占满,其他的控件就没有可放置的位置了。
  • 同样的道理,如果LinearLayout 的排列方向是 vertical,内部的控件就不能将高度指定为match_parent。
  • gravity和layout_gravity的区别就是gravity是指当前控件内部的内容的排列方式,而后者则是指当前控件相对于父布局的的排列方式。示例:效果见下图左边的图
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:text="Button 1" />
    <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:text="Button 2" />
    <Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:text="Button 3" />
    </LinearLayout>

                    

  • android:layout_weight属性是指所设置的控件对剩余空间的权重(权重越小,所占空间越大)。示例:效果见上图右边的图

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <EditText
    android:id="@+id/input_message"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:hint="Type something"
    />
    <Button
    android:id="@+id/send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Send"
    />
    </LinearLayout> 

  注意这里我们将edittext的权重设为1,而width设为0只是一个规范化写法,因为send的width是wrapcontent,所以这一行的剩余空间分到所有权重之和(1)上,1/1即为edittext所占比例。

RelativeLayout

RelativeLayout 又称作相对布局,也是一种非常常用的布局。就和名字一样,这种布局内的控件排列全部按照相对的父布局或其它子控件等的方式进行布局。示例:效果见下图左边的图

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button 2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button 3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Button 4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Button 5" />
</RelativeLayout>
 

                  

  • 相对于子控件的示例:效果见上图右边的图
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button 3" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button3"
android:layout_toLeftOf="@id/button3"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button3"
android:layout_toRightOf="@id/button3"
android:text="Button 2" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button3"
android:layout_toLeftOf="@id/button3"
android:text="Button 4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button3"
android:layout_toRightOf="@id/button3"
android:text="Button 5" />
</RelativeLayout>
  • RelativeLayout中还有另外一组相对于控件进行定位的属性,android:layout_alignLeft表示让一个控件的左边缘和另一个控件的左边缘对齐,android:layout_alignRight表示让一个控件的右边缘和另一个控件的右边缘对齐,还有android:layout_alignTop、android:layout_alignBottom

FrameLayout

这种布局没有任何的定位方式,所有的控件都会摆放在布局的左上角,该布局类型主要运用于碎片处理。示例效果见下图左边的图

 
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
/>
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
</FrameLayout>

              

 
TableLayout
  TableLayout允许我们使用表格的方式来排列控件。例如,设计一个登录界面,允许用户输入账号密码后登录,示例效果见上图右边的图
 <pre name="code" class="java">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Account:" />
<EditText
android:id="@+id/account"
android:layout_height="wrap_content"
android:hint="Input your account" />
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Password:" />
<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</TableRow>
<TableRow>
<Button
android:id="@+id/login"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="Login" />
</TableRow>
</TableLayout>
  在TableLayout 中每加入一个 TableRow 就表示在表格中添加了一行,然后在TableRow中每加入一个控件,就表示在该行中加入了一列,TableRow中的控件是不能指定宽度的。使用android:layout_span="2"让登录按钮占据两列的空间,android:stretchColumns="1"使得第二个控件自动拉伸并占慢剩余空间。

3、如何自定义控件
参考:http://www.cnblogs.com/0616--ataozhijia/p/4003380.html
 
4、定制ListView的界面(P129)

5、ListView的效率优化(P133)

  ListView的效率瓶颈存在的原因在于:

    • getView()方法中每次都将布局进行重新加载一次,当ListView快速滚动时导致加载缓慢
    • 每次在getView()方法中还是会调用View的findViewById()方法来获取一次控件的实例

  对于第一个问题,我们可以通过重写getView()方法,getView()方法中的convertView参数可以用于将之前加载好的布局进行缓存,以便以后进行重用

  对于第二个问题,我们可以借助一个ViewHolder来对这部分进行优化,用ViewHolder对控件的实例进行缓存

 
6、单位和尺寸
  • px:就是pixel,像素,即屏幕中可以显示的最小元素单元,大小是相对屏幕的分辨率而言的。
  • pt:就是point,磅数,是印刷行业常用单位,等于1/72英寸,绝对大小。一般pt用作字体的单位来使用
  • dp:就是device independent pixels,设备独立像素,和px相比,它在不同密度的屏幕中的显示比例保持一致。(常用)
  • sp:就是scaled pixels,可伸缩像素,和pt相比,其区别和dp和px的区别一样,解决文字大小适配的问题(常用于文字)
 
 
 
 

Android基础总结(3)——UI界面布局的更多相关文章

  1. Flutter实战视频-移动电商-30.列表页_商品列表UI界面布局

    30.列表页_商品列表UI界面布局 小程序里面的布局方式 小程序的图片上这里使用的是warp布局,因为首页里面火爆专区,已经用过了warp来布局了. 所以这里我们没有必要再讲一遍,这里我们使用List ...

  2. 2.3 使用Android Studio 简单设计UI界面

    首先 创建一个新的项目找到app 文件目录下的layout的 activity_main.xml 因为Android Studio 是可视化的,所有操作都可以在图形界面进行. 该res 界面当中  d ...

  3. Android基础之——startActivityForResult启动界面并返回数据,上传头像

    在android应用的开发过程中,常常会出现启动一个界面后填写部分内容后带着数据返回启动前的界面,最典型的应用就是登录过程.在非常多应用程序的模块中,都有"我的"这个模块,在未登录 ...

  4. Android基础总结(二)布局,存储

    常见布局 相对布局 RelativeLayout 组件默认左对齐.顶部对齐 设置组件在指定组件的右边 android:layout_toRightOf="@id/tv1" 设置在指 ...

  5. Flutter移动电商实战 --(30)列表页_商品列表UI界面布局

    小程序里面的布局方式 小程序的图片上这里使用的是warp布局,因为首页里面火爆专区,已经用过了warp来布局了. 所以这里我们没有必要再讲一遍,这里我们使用ListView,我们把它布局成下图这种形式 ...

  6. Android开发1:基本UI界面设计——布局和组件

    前言 啦啦啦~本学期要开始学习Android开发啦~ 博主在开始学习前是完完全全的小白,只有在平时完成老师要求的实验的过程中一步一步学习~从此篇博文起,博主将开始发布Android开发有关的博文,希望 ...

  7. Android开发精彩博文收藏——UI界面类

    本文收集整理Android开发中关于UI界面的相关精华博文,共大家参考!本文不定期更新! 1. Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各 ...

  8. WPF基础知识、界面布局及控件Binding(转)

    WPF是和WinForm对应的,而其核心是数据驱动事件,在开发中显示的是UI界面和逻辑关系相分离的一种开放语言.UI界面是在XAML语言环境下开发人员可以进行一些自主设计的前台界面,逻辑关系还是基于c ...

  9. WPF基础知识、界面布局及控件Binding

    WPF是和WinForm对应的,而其核心是数据驱动事件,在开发中显示的是UI界面和逻辑关系相分离的一种开放语言.UI界面是在XAML语言环境下开发人员可以进行一些自主设计的前台界面,逻辑关系还是基于c ...

随机推荐

  1. 黄聪:C#如何操作JSON数据(读取、分析)

    使用开源的类库Newtonsoft.Json(下载地址http://json.codeplex.com/).下载后加入工程就能用.通常可以使用JObject, JsonReader, JsonWrit ...

  2. protobuffer序列化

    一. 描述对象的proto文件 第一行package:对象经过protobuffer编译后形成java文件,这个文件放在按照package新建的文件夹内 java_package:java类的包名 j ...

  3. Linux使用笔记: 定制core dump文件的文件名

    在开发过程中,当一个Linux程序异常退出时,我们可以通过core文件来分析它异常的详细原因.缺省情况下,Linux在程序异常时不产生core文件,要想让程序异常退出时产生core dump文件,需要 ...

  4. PO_PO系列 - 询价报价单管理分析(案例)

    2014-07-01 Created By BaoXinjian

  5. ibatis插入数据后返回自增长的主键

    insert into testTable ( activity_id,activity_title values ( #{activityId,jdbcType=INTEGER}, #{activi ...

  6. JAVA 数组排序

    一.数组升序排序 实例: import java.util.Arrays; //导入数组处理 public class Test{ public static void main(String[] a ...

  7. rpm 与压缩解压缩

    rpm包redhat package manager 逐渐地被linux发行版本使用的安装包 rpm -qa 查询所有linux中安装了那些rpm包 rpm -q mysql 查看mysql 包的信息 ...

  8. Guava 10-散列

    概述 Java内建的散列码[hash code]概念被限制为32位,并且没有分离散列算法和它们所作用的数据,因此很难用备选算法进行替换.此外,使用Java内建方法实现的散列码通常是劣质的,部分是因为它 ...

  9. $.extend()和$.fn.extend()用法和区别

    $.extend()和$.fn.extend()用法和区别: 在自己制作插件的时候会经常用到$.extend()和$.fn.extend()两个函数,无论从外观还是作用都非常的类似,但是实际上它们的区 ...

  10. JAVA坏境变量中的JAVA_HOME path classpath 的设置与作用。

    在把jdk安装到计算机中之后,我们来进行设置使java环境能够使用. 首先右键点我的电脑.打开属性.然后选择“高级”里面的“环境变量”,在新的打开界面中的系统变量需要设置三个属性“JAVA_HOME” ...