绝对布局(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. 【译】 AWK教程指南 2概述

    2.1 为什么用AWK 由于awk具有上述特色,在问题处理的过程中,可轻易使用awk来撰写一些小工具:这些小工具并非用来解决整个大问题,它们只扮演解决个别问题过程的某些角色,可通过Shell所提供的p ...

  2. bzoj 2716 天使玩偶(CDQ分治,BIT)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=29234 [题意] 询问当前点与已知点的最小曼哈顿距离. [思路 ...

  3. 机器学习-----线性回归浅谈(Linear Regression)

    Linear Regreesion          在现实生活中普遍存在着变量之间的关系,有确定的和非确定的.确定关系指的是变量之间可以使用函数关系式表示,还有一种是属于非确定的(相关),比如人的身 ...

  4. 在iptables中添加vnc访问规则

    iptables -I INPUT -p tcp --dport 5800|5900|5801|5901 -j ACCEPT service iptales save

  5. leetcode@ [274/275] H-Index & H-Index II (Binary Search & Array)

    https://leetcode.com/problems/h-index/ Given an array of citations (each citation is a non-negative ...

  6. leetcode@ [62/63] Unique Paths II

    class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleG ...

  7. Hadoop项目开发环境搭建(Eclipse\MyEclipse + Maven)

    写在前面的话 可详细参考,一定得去看 HBase 开发环境搭建(Eclipse\MyEclipse + Maven) Zookeeper项目开发环境搭建(Eclipse\MyEclipse + Mav ...

  8. Spark生态

  9. linux大于2T的磁盘使用GPT分区方式

    MBR(Master Boot Record)(主引导记录)和GPT(GUID Partition Table)(GUID意为全局唯一标识符)是在磁盘上存储分区信息的两种不同方式 对于传统的MBR分区 ...

  10. Block的引用循环问题 (ARC & non-ARC)

    2010年WWDC发布iOS4时Apple对Objective-C进行了一次重要的升级:支持Block.说到底这东西就是闭包,其他高级语音例如Java和C++已有支持,第一次使用Block感觉满简单好 ...