安卓app开发-05-Android xml布局详细介绍

  • 虽然说有 墨刀,墨客 这些图形化开发工具来做 Android 的界面设计,但是我们还是离不开要去学习做安卓原生app,学习 xml 布局还是必要的

(1)准备

  • 首先我们要了解 android 到底有那些布局,和每个布局类型的区别
  • 学习时最好打开 Android Studio 打开 xml 文件对应看一下
  • 配置参数的详细含义不用着急全部理解,放在文章后面,可收藏做查阅【可通过目录跳转】

(2)学习目标

  • 学习下xml的布局文件具体写法。这一节我们要绘制如下图所示的界面

(3)线性布局 LinearLayout

  • 线性布局分两种。一种是水平布局,一种是垂直布局。下面我们根据上图举例子
  • 上图代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_height="wrap_content" android:text="@string/note_title"
android:layout_width="wrap_content" android:padding="10dp"></TextView>
<LinearLayout android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1">
<EditText android:id="@+id/EditText02" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="left"
android:hint="@string/edithint"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="2"
android:gravity="center"
android:orientation="horizontal">
<ImageButton android:id="@+id/ImageButton01" android:layout_width="72dp" android:layout_height="72dp" android:src="@drawable/sketchy_paper_003" android:layout_margin="3dp"></ImageButton> <ImageButton android:id="@+id/ImageButton02" android:layout_width="72dp" android:layout_height="72dp" android:src="@drawable/sketchy_paper_004" android:layout_margin="3dp"></ImageButton> <ImageButton android:id="@+id/ImageButton03" android:layout_width="72dp" android:layout_height="72dp" android:src="@drawable/sketchy_paper_007" android:layout_margin="3dp"></ImageButton> <ImageButton android:id="@+id/ImageButton04" android:layout_width="72dp" android:layout_height="72dp" android:src="@drawable/sketchy_paper_011" android:layout_margin="3dp"></ImageButton> </LinearLayout> </LinearLayout>
  • 可以看到,上图是由三部分组成。在大的LinearLayout从上而下垂直分布着三个内容:TextView,LinearLayout,LinearLayout。所以总体的 LinearLayout 是垂直布局
  • 下面我们来看水平布局
  • 其实就是上图中的最下面那个 LinearLayout。四个图标平行排列。

    android:orientation="horizontal"

(4)相对布局 RelativeLayout

  • 这个布局相对简单一点。一般来讲利用ADT自己拖放按钮就可以。基本上可以随意布局。如下图所示

  • 上图代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" /> <Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:text="Button" /> <Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="97dp"
android:text="Button" /> <Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button3"
android:layout_marginTop="89dp"
android:text="Button" /> <Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_alignTop="@+id/button4"
android:text="Button" /> </RelativeLayout>
  • layout_marginBottom是指控件边以外空下的距离,比如Button1和Button2垂直显示,将Button1中layout_marginBottom = 10dp,那么Button1与Button2之间将有10dp距离
  • 下面这两句是居左显示和居右显示

    android:layout_alignParentLeft="true"

    android:layout_alignParentTop="true"
  • 【提示】:相对视图应该是最有用的,具体的操作比较复杂,更多的是通过图形界面拖拉,再用代码微调

(5)帧布局 FrameLayout

  • 这个布局很简单,而且感觉有点二二的,哈哈!就是控件一个挨一个在左上角罗列

  • 上图代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" /> <Button
android:id="@+id/button2"
android:layout_width="126dp"
android:layout_height="135dp"
android:text="Button" /> <Button
android:id="@+id/button3"
android:layout_width="194dp"
android:layout_height="232dp"
android:text="Button" /> </FrameLayout>

(6)绝对布局 AbsoluteLayout

  • 绝对布局比较容易使用,就是以左上方为原点建立坐标系。每个控件用layout_x和layout_y表示位置。但是据说这种布局比较刚性,不容易适配各种终端,所以要慎用!

  • 上图代码:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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_x="44dp"
android:layout_y="18dp"
android:text="Button" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="122dp"
android:layout_y="173dp"
android:text="Button" /> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="36dp"
android:layout_y="133dp"
android:text="Button" /> </AbsoluteLayout>

(7)表格布局 TableLayout

  • TableLayout有点像一个表格或是矩阵。在布局中加入TableRow,它的属性是horizontal所以每个TableRow只能横放。它里面的每个控件的高都是一样的。下图所示,是加入了一个TableRow和里面的控件

  • 上图代码:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" /> <Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" /> <Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" /> <Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" /> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow> </TableLayout>

xml 配置参数大全

  • 第一类:属性值为true或false

    android:layout_centerHrizontal 水平居中

    android:layout_centerVertical 垂直居中

    android:layout_centerInparent 相对于父元素完全居中

    android:layout_alignParentBottom 贴紧父元素的下边缘

    android:layout_alignParentLeft 贴紧父元素的左边缘

    android:layout_alignParentRight 贴紧父元素的右边缘

    android:layout_alignParentTop 贴紧父元素的上边缘

    android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物

  • 第二类:属性值必须为id的引用名

    android:layout_below 在某元素的下方

    android:layout_above 在某元素的的上方

    android:layout_toLeftOf 在某元素的左边

    android:layout_toRightOf 在某元素的右边

    android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐

    android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐

    android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐

    android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

  • 第三类:属性值为具体的像素值,如30dip,40px

    android:layout_marginBottom 离某元素底边缘的距离

    android:layout_marginLeft 离某元素左边缘的距离

    android:layout_marginRight 离某元素右边缘的距离

    android:layout_marginTop 离某元素上边缘的距离

    EditText的android:hint 设置EditText为空时输入框内的提示信息。

    android:gravity 

    android:gravity属性是对该view 内容的限定.比如一个button 上面的text. 你可以设置该text 在view的靠左,靠右等位置.以button为例,android:gravity="right"则button上面的文字靠右

    android:layout_gravity

    android:layout_gravity是用来设置该view相对与起父view 的位置.比如一个button 在linearlayout里,你想把该button放在靠左、靠右等位置就可以通过该属性设置.以button为例,android:layout_gravity="right"则button靠右

    android:scaleType:

    android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType / android:scaleType值的意义区别:

    CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示

    CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)

    CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽

    FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示

    FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置

    FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置

    FIT_XY / fitXY 把图片不按比例扩大/缩小到View的大小显示

    MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。

    ** 要注意一点,Drawable文件夹里面的图片命名是不能大写的。


android:id 为控件指定相应的ID

android:text 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串

android:gravity 指定View组件的对齐方式,比如说居中,居右等位置 这里指的是控件中的文本位置并不是控件本身

android:layout_gravity 指定Container组件的对齐方式.比如一个button 在linearlayout里,你想把该button放在靠左、靠右等位置就可以通过该属性设置.以button为 例,android:layout_gravity="right"则button靠右

android:textSize 指定控件当中字体的大小

android:background 指定该控件所使用的背景色,RGB命名法

android:width 指定控件的宽度

android:height 指定控件的高度

android:layout_width 指定Container组件的宽度

android:layout_height 指定Container组件的高度

android:layout_weight View中很重要的属性,按比例划分空间

android:padding* 指定控件的内边距,也就是说控件当中的内容

android:sigleLine 如果设置为真的话,则控件的内容在同一行中进行显示

android:scaleType 是控制图片如何resized/moved来匹对ImageView的siz

android:layout_centerHrizontal 水平居中

android:layout_centerVertical 垂直居中

android:layout_centerInparent 相对于父元素完全居中

android:layout_alignParentBottom 贴紧父元素的下边缘

android:layout_alignParentLeft 贴紧父元素的左边缘

android:layout_alignParentRight 贴紧父元素的右边缘

android:layout_alignParentTop 贴紧父元素的上边缘

android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物

android:layout_below 在某元素的下方

android:layout_above 在某元素的的上方

android:layout_toLeftOf 在某元素的左边

android:layout_toRightOf 在某元素的右边

android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐

android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐

android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐

android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

android:layout_marginBottom 离某元素底边缘的距离

android:layout_marginLeft 离某元素左边缘的距离

android:layout_marginRight 离某元素右边缘的距离

android:layout_marginTop 离某元素上边缘的距离

android:paddingLeft 本元素内容离本元素右边缘的距离

android:paddingRight 本元素内容离本元素上边缘的距离

android:hint 设置EditText为空时输入框内的提示信息

android:LinearLayout 它确定了LinearLayout的方向,其值可以为vertical,表示垂直布局horizontal, 表示水平布局


android:interpolator

可能有很多人不理解它的用法,文档里说的也不太清楚,其实很简单,看下面:interpolator定义一个动画的变化率(the rate of change)。这使得基本的动画效果(alpha, scale, translate, rotate)得以加速,减速,重复等。用通俗的一点的话理解就是:动画的进度使用 Interpolator 控制。interpolator 定义了动画的变化速度,可以实现匀速、正加速、负加速、无规则变加速等。Interpolator 是基类,封装了所有 Interpolator 的共同方法,它只有一个方法,即 getInterpolation (float input),该方法 maps a point on the timeline to a multiplier to be applied to the transformations of an animation。Android 提供了几个 Interpolator 子类,实现了不同的速度曲线,如下:

AccelerateDecelerateInterpolator 在动画开始与介绍的地方速率改变比较慢,在中间的时侯加速

AccelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始加速

CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线

DecelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始减速

LinearInterpolator 在动画的以均匀的速率改变

对于 LinearInterpolator ,变化率是个常数,即 f (x) = x.

public float getInterpolation(float input) {

return input;

}

Interpolator其他的几个子类,也都是按照特定的算法,实现了对变化率。还可以定义自己的 Interpolator 子类,实现抛物线、自由落体等物理效果

更多文章链接:安卓app开发


- 本笔记不允许任何个人和组织转载

安卓app开发-05-Android xml布局详细介绍的更多相关文章

  1. 安卓app开发-03-项目的基本开发步骤

    android项目的基本开发步骤 这里分享一下开发 安卓 app 的流程,当然有些感觉不必要,其实不然,前期工作也是极为重要的额,就像开发的时候如果目标不对的话,到后期后很迷的,所以一定要提前做好规划 ...

  2. 安卓app开发-01-开发工具及环境配置

    安卓app开发-01-开发工具及环境配置 请大家根据推荐指数和自己的意愿选择 使用 Android Studio 1.可以使用 Android Studio 下载地址:http://www.andro ...

  3. 安卓app开发-02-安卓app快速开发

    安卓app开发-02-安卓app快速开发 上一篇介绍了安卓 app 开发的工具和环境配置,本篇不涉及编程技术,适合小团队快速高效开发 APP制作流程 当有一个APP创意,该如何实现呢?是花数十万找AP ...

  4. 学习笔记:APP切图那点事儿–详细介绍android和ios平台

    学习笔记:APP切图那点事儿–详细介绍android和ios平台 转载自:http://www.woofeng.cn/articles/168.html   版权归原作者所有 作者:亚茹有李 原文地址 ...

  5. android xml布局文件属性说明

    android xml布局文件属性说明 [摘]android xml布局文件属性说明 LinearLayout和RelativeLayout 共有属性:java代码中通过btn1关联次控件androi ...

  6. Android SQLite 数据库详细介绍

    Android SQLite 数据库详细介绍 我们在编写数据库应用软件时,需要考虑这样的问题:因为我们开发的软件可能会安装在很多用户的手机上,如果应用使用到了SQLite数据库,我们必须在用户初次使用 ...

  7. 【软件配置】JDK+AndroidStudio4.1开发安卓APP环境安装和配置教程详细

    目录 一.专业名词 二.搭建前资源准备 2.1 JDK资源下载 2.2 AndroidStudio下载 三.安装 3.1 JDK安装配置 3.2 AndroidStudio安装 四.创建安卓APP工程 ...

  8. 安卓APP开发的初步了解

    今天成功安装了Android Studio 并且对APP的开发框架结构进行了初步了解 如上图:app基本结构情况 下面来仔细解释一下各个方面目录的作用 首先 manifests目录:包含Android ...

  9. 当前主流的安卓APP开发IDE

    什么是主流的开发安卓APP的方式? 我是去年4月份接触的Android开发,因此特别有感触,可以明显的感受到安卓APP主流开发方式的改变. 去年,2015年年初,各大安卓开发群大部分大牛在用Eclip ...

随机推荐

  1. 【编程技术-Shell】AWK使用大全

    1.  AWK中输出特殊字符 输出单引号 涉及到转义字符,但是在使用普通的方法进行转义时,会遇到下面的问题 正确的方法:'\'',使用单引号将转义字符括起来,然后后面加上单引号 输出其他特殊字符 输出 ...

  2. Spring Boot Starter列表

    转自:http://blog.sina.com.cn/s/blog_798f713f0102wiy5.html Spring Boot Starter 基本的一共有43种,具体如下: 1)spring ...

  3. java_Proxy动态代理_AOP

    讲了JDK里使用Proxy动态代理的机制,详细过程. 切面类TransactionHandler需要实现InvocationHaandler接口,实现它的invoke方法. 项目目录: User类代码 ...

  4. Path;Paths和Files;FileVisitor

    package filet; import java.io.FileOutputStream; import java.nio.file.FileStore; import java.nio.file ...

  5. 创建自己的加密货币MNC——以太坊代币(二)

    创建一个基于以太坊平台的分红币MNC,根据持有的代币数量,进行分红的算法.github地址: https://github.com/lxr1907/MNC 1.使用以太坊根据比例换购token MNC ...

  6. CentOS 6.4下安装JIRA6.3.6破解汉化

    JIRA产品非常完善且功能强大,安装配置简单,多语言支持.界面十分友好,和其他系统如CVS.Subversion(SVN).VSS.LDAP.邮件服务整合得相当好,文档齐全,可用性以及可扩展性方面都十 ...

  7. transform旋转变换效果

    div{ transform:rotate(7deg); -ms-transform:rotate(7deg); /* IE 9 */ -moz-transform:rotate(7deg); /* ...

  8. goahead3.6.3就基本使用(后台上传信息到html页面),高手请忽略

    声明:这里面的代码均为网上找的然后有小小的改动,并非原创.但文章为原创 一..编译 1.1,.下载:进入http://embedthis.com/goahead/下载goahead3.6.3(2017 ...

  9. *2.3.2_加入env

    在验证平台中加入reference model.scoreboard等之前,思考一个问题:假设这些组件已经定义好了,那么在验证平台的什么位置对它们进行实例化呢?在top_tb中使用run_test进行 ...

  10. Django 用模板来节省公共代码,

          如果我们有一个后台网站,或者显示个人信息的网站 标题logo,边框等信息的代码都是固定的,只有部分区域是改变, 我们如果用Django来写html的话就会重复代码.这时候我们只要用模板的方 ...