Android Layout之五:AbsoluteLayout

本文详细介绍Android中的AbsoluteLayout(绝对布局),通过实例展示了如何精确控制视图的位置,包括使用坐标定位和计算视图尺寸的方法。

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

Android Layout之五:AbsoluteLayout

 

 

AbsoluteLayout也就是绝对布局,又称坐标布局,在布局上灵活性较大,也较复杂,另外由于各种手机屏幕尺寸的差异,给开发人员带来较多困难。


用坐标布局时,需要注意坐标原点为屏幕左上角,这和电脑屏幕的设置时一样一样的;添加视图时,要精确的计算每个视图的像素大小,最好先在纸上画草图,并所有元素的像素定位计算好。

 

这里我选取了320*480像素的标准屏幕,google示例图片尺寸为276*110。

 



 

在drawable目录下防止上面的google图片,图片文件名为google.png,如下图所示。



 

 

main.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout    xmlns:android="http://schemas.android.com/apk/res/android"    
	android:layout_width="wrap_content"    
	android:layout_height="wrap_content"
	>        
	<ImageView        
		android:layout_width="wrap_content"        
		android:layout_height="wrap_content"        
		android:src="@drawable/google"        
		android:layout_x="22px"        
		android:layout_y="50px"
		/>    
	<TextView        
		android:layout_width="wrap_content"        
		android:layout_height="wrap_content"        
		android:text="I love Android"        
		android:layout_x="100px"        
		android:layout_y="140px"
		/>    
	<TextView        
		android:layout_width="wrap_content"        
		android:layout_height="wrap_content"        
		android:text="I love Goolge"        
		android:layout_x="120px"        
		android:layout_y="195px"
		/>    
	<TextView        
		android:layout_width="wrap_content"        
		android:layout_height="wrap_content"        
		android:text="I love the world"        
		android:layout_x="140px"        
		android:layout_y="215px"
		/>
</AbsoluteLayout>

 

 

 

结果如下图:



 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值