如何在Android中没有绝对布局的情况下将图像放在一起?

时间:2012-08-22 13:14:02

标签: android android-layout

基本上标题不言自明。 我想把元素放在不同的元素之上。我可以使用绝对布局,但据说不赞成使用。

还有其他办法吗?如果不是,为什么他们会弃用AbsoluteLayout?

2 个答案:

答案 0 :(得分:7)

RelativeLayout的默认行为会将左上角的所有元素叠加在一起,所以这个布局:

<RelativeLayout
android:width="fill_parent"
android:height="fillparent">

<ImageView
android:width="wrap_content"
android:height="wrap_content"
android:src="@drawable/img1" />

<ImageView
android:width="wrap_content"
android:height="wrap_content"
android:src="@drawable/img2" />

</RelativeLayout>

将两个图像叠加在屏幕左上角的另一个图像上。相对布局提供了许多其他属性,允许您将视图移动到屏幕上的不同位置。但是这个想法是它可以更好地扩展到不同的大小,因为你不会硬编码你希望元素去的地方的x,y值。

答案 1 :(得分:0)

我很确定你可以用RelativeLayout进行这种操作。

这是doc: http://developer.android.com/reference/android/widget/RelativeLayout.html