绝对布局中android layout,Android 自学之绝对布局 AbsoluteLayout

本文介绍了Android中的绝对布局,它类似Java AWT的空布局,需开发者通过X、Y坐标控制组件位置。使用时可通过layout_x和layout_y属性指定组件坐标,并给出示例代码。此外,还介绍了Android常用的距离单位,如px、dip、sp等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

绝对布局(AbsoluteLayout),绝对布局就像java

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

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

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

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

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

layout/main.xml

fcecaa27ea5212ceb9bf034c36bfbf34.gif1 <?xml version="1.0" encoding="utf-8"?>

2

3 android:orientation="vertical"

4 android:layout_width="fill_parent"

5 android:layout_height="fill_parent"

6 >

7

8

10 android:layout_y="20dip"

11 android:layout_width="wrap_content"

12 android:layout_height="wrap_content"

13 android:text="用户名:"

14 />

15

16

18 android:layout_y="15dip"

19 android:layout_width="wrap_content"

20 android:width="200px"

21 android:layout_height="wrap_content"

22 />

23

24

26 android:layout_y="80dip"

27 android:layout_width="wrap_content"

28 android:layout_height="wrap_content"

29 android:text="密 码:"

30 />

31

32

34 android:layout_y="75dip"

35 android:layout_width="wrap_content"

36 android:width="200px"

37 android:layout_height="wrap_content"

38 android:password="true"

39 />

40

41

43 android:layout_y="135dip"

44 android:layout_width="wrap_content"

45 android:layout_height="wrap_content"

46 android:text="登 录"

47 />

48

fcecaa27ea5212ceb9bf034c36bfbf34.gif

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

5c3b64a80c8a55e8c36899bd2ba2d7ae.png

主程序

com.example.absolutelayouttest.MainActivity.java

fcecaa27ea5212ceb9bf034c36bfbf34.gif1 packagecom.example.absolutelayouttest;2

3 importandroid.support.v7.app.ActionBarActivity;4 importandroid.support.v7.app.ActionBar;5 importandroid.support.v4.app.Fragment;6 importandroid.os.Bundle;7 importandroid.view.LayoutInflater;8 importandroid.view.Menu;9 importandroid.view.MenuItem;10 importandroid.view.View;11 importandroid.view.ViewGroup;12 importandroid.os.Build;13

14 public class MainActivity extendsActionBarActivity {15

16 @Override17 protected voidonCreate(Bundle savedInstanceState) {18 super.onCreate(savedInstanceState);19 setContentView(R.layout.main);20

21

22 }23

24 }

fcecaa27ea5212ceb9bf034c36bfbf34.gif

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

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

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

px(像素):每一个像素对应屏幕上的一个点。

dip或dp(device independent

pixels,设备独立像素):一种基于屏幕密度的抽象单位。例如:在每英寸160点显示器上,1dip=1px。但随着屏幕密度的改变,dip与px的换算会发生改变。

sp(scaled pixels,比例像素):主要处理字体大小,可以根据用户的字体大小首选项进行缩放。

in(英寸):标准长度单位。

mm(毫米):标准长度单位。

pt(磅):标准长度单位,1/72英寸。

原文:http://www.cnblogs.com/Yang-jing/p/3747124.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值