绝对布局(AbsoluteLayout),绝对布局就像java AWT中的空布局;所谓的绝对布局就是Android不提供任何的布局控制,而是有开发人员自己通过X坐标和Y坐标来控制组件的位置。当使用绝对布局作为布局容器的时候,布局容器不在管理子容器的位置,大小(这些都需要开发人员自己控制)。

在使用绝对布局的时,每个组件都可以使用这两个XML属性

layout_x:指定该子组件的X坐标;

layout_y:指定该子组件的Y坐标;

以一个学习的例子做范例:

layout/main.xml

 <?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 定义一个文本框,使用绝对定位 -->
<TextView
android:layout_x="20dip"
android:layout_y="20dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
/>
<!-- 定义一个文本编辑框,使用绝对定位 -->
<EditText
android:layout_x="80dip"
android:layout_y="15dip"
android:layout_width="wrap_content"
android:width="200px"
android:layout_height="wrap_content"
/>
<!-- 定义一个文本框,使用绝对定位 -->
<TextView
android:layout_x="20dip"
android:layout_y="80dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
/>
<!-- 定义一个文本编辑框,使用绝对定位 -->
<EditText
android:layout_x="80dip"
android:layout_y="75dip"
android:layout_width="wrap_content"
android:width="200px"
android:layout_height="wrap_content"
android:password="true"
/>
<!-- 定义一个按钮,使用绝对定位 -->
<Button
android:layout_x="130dip"
android:layout_y="135dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登 录"
/>
</AbsoluteLayout>

上面代码所展示的效果图:

主程序

com.example.absolutelayouttest.MainActivity.java

 package com.example.absolutelayouttest;

 import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build; public class MainActivity extends ActionBarActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); } }

上面的主程序就只是把layout/main.xml布局显示了一下。

layout/main.xml界面布局中指定的各组件android:layout_x、android:layout_y属性时指定了形如20dip的属性值。

Android中一般支持如下常用的距离单位:

  1. px(像素):每一个像素对应屏幕上的一个点。
  2. dip或dp(device independent pixels,设备独立像素):一种基于屏幕密度的抽象单位。例如:在每英寸160点显示器上,1dip=1px。但随着屏幕密度的改变,dip与px的换算会发生改变。
  3. sp(scaled pixels,比例像素):主要处理字体大小,可以根据用户的字体大小首选项进行缩放。
  4. in(英寸):标准长度单位。
  5. mm(毫米):标准长度单位。
  6. pt(磅):标准长度单位,1/72英寸。

Android 自学之绝对布局 AbsoluteLayout的更多相关文章

  1. Android 自学之相对布局 RelativeLayout

    相对布局(RelativeLayout),相对布局容器内子组件的位置总是相对兄弟组件.父容器来决定的. RelativeLayout的XML属性及相关方法说明 XML属性 相关方法 说明 androi ...

  2. Android 自学之表格布局 TableLayout

    表格布局(TableLayout),表格布局采用行.列的形式来管理UI组件,TableLayout并不需要明确的声明多少行,多少列,而是通过TableRow.其他组件来控制表格的行数和列数. 每次想T ...

  3. Android 自学之线性布局 LinearLayout

    线性布局(LinearLayout),线性布局有点想AWT编程里面的FolwLayout,他们都会将容器里面的组件挨个的排列起来. 他们最大的区别在于:Android的线性布局不会换行:AWT里面的F ...

  4. Android 自学之帧布局 FrameLayout

    帧布局(FrameLayout)直接继承了ViewGroup组件: 帧布局容器为每一个加入其中的组件都创建了一个空白的区域,这个区域我们称之为一帧,所有每个组件都占据一帧,这些都会根据gravity属 ...

  5. Android开发-之五大布局

    在html中大家都知道布局是什么意思了,简单来说就是将页面划分模块,比如html中的div.table等.那么Android中也是这样的.Android五大布局让界面更加美化,开发起来也更加方便.当然 ...

  6. 【Android自学日记】五大布局常用属性

    线性布局(LinearLayout)常用属性: android:orientation="vertical"--决定子类控件的排布方式(vertical垂直:horizontal水 ...

  7. Android开发自学笔记(Android Studio)—4.1布局组件

    一.引言 Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.在Android4.0之前,我们通常说 ...

  8. Android精通:TableLayout布局,GridLayout网格布局,FrameLayout帧布局,AbsoluteLayout绝对布局,RelativeLayout相对布局

    在Android中提供了几个常用布局: LinearLayout线性布局 RelativeLayout相对布局 FrameLayout帧布局 AbsoluteLayout绝对布局 TableLayou ...

  9. Android成长日记-五大布局

    1. 五布局之线性布局LinearLayout 特点:它包含的子控件将以横向或竖向的方式排列 ps:android:gravity=”center|bottom”(gravity允许多级联用) Tip ...

随机推荐

  1. e2e 自动化集成测试 架构 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (五) 如何让窗体记录登录状态Session

    在写自动化测试的Node.js脚本时, 时常需要测试所写的case, 可能都需要去重新登录一遍,这将相当的耗时, 好在Selenium都借了Session的机制, 如果在最初的浏览器没有关闭的情况下, ...

  2. Idiomatic Python手记一: average in FP way

    方法一: import operator def average(*args): return reduce(operator.add, args) / len(args) if args else ...

  3. 【暑假】[实用数据结构]KMP

    KMP算法 KMP算法是字符串匹配算法,可以在O(n)的时间完成,算法包含两部分,分别是:构造适配函数与两串匹配. 失配边的使用大大提高了算法效率,可以理解为已经成功匹配的字符不在重新匹配,因为我们已 ...

  4. C++多线程框架-----Mutex互斥和Sem信号量

           互斥和信号量是多线程编程的两个基础,其原理就不详细说了,大家去看看操作系统的书或者网上查查吧. 对于互斥的实现,无论什么操作系统都离不开三个步骤 1.初始化互斥锁 2.锁操作 3.解锁操 ...

  5. MATLAB绘图与图形处理

    参考:http://www.cnblogs.com/djcsch2001/tag/MATLAB/  matlab部分写的不错! 7.2  三维图形 7.2.1  三维曲线.面填色命令 命令1  com ...

  6. lua协程实现简析

    协程,简单来说就是新创建一个协助程序(co = coroutine.create(func)),然后需要手动去启动它(coroutine.resume(co)),在它最终退出之前,它有可能暂停多次返回 ...

  7. Docker系列(七)Shipyard安装及介绍

    Shipyard 是一个基于 Web 的 Docker 管理工具,支持多 host,可以把多个 Docker host 上的 containers 统一管理:可以查看 images,甚至 build ...

  8. 【openstack报错】【metadata问题】‘http://169.254.169.254/2009-04-04/meta-data/instance-id’ failed : url error [[Errno 111] Connection refused]

    [时间]2014年2月25日 [平台]ubuntu 12.04.3 openstack havana  with nova-network in multi-host [日志]实例启动时输出的日志内容 ...

  9. Windows下Android SDK Manage下载速度缓慢的解决方法

    在SDK Manager下Tools->Options打开了SDK Manager的Settings,选中“Force https://… sources to be fetched using ...

  10. 家庭局域网接入Internet

     接入Internet  建立宽带连接 步骤一:单击"网络",再属性 步骤二:单击"更改适配器设置" 步骤三:选择"宽带连接" 步骤四:输入 ...