Android-相对性问题

Android-相对性问题,android,android-layout,Android,Android Layout,在relativelayout中,图像基于xml文件层叠在另一个图像之上。您如何指示最上面应该是什么图像 有没有办法用代码而不是xml来实现这一点 我正在附加我的XML文件以供参考: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"

在relativelayout中,图像基于xml文件层叠在另一个图像之上。您如何指示最上面应该是什么图像

有没有办法用代码而不是xml来实现这一点

我正在附加我的XML文件以供参考:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
>

<ImageView
    android:id="@+id/img1"
    android:src="@drawable/payarrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<ImageView
    android:id="@+id/img2"
    android:src="@drawable/chargearrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
<ImageView
    android:id="@+id/landingDockLeft"
    android:src="@drawable/landingdock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
<ImageView
    android:id="@+id/landingDockRight"
    android:src="@drawable/landingdock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />  


android:layout\u alignParentTop=“true”

在xml中,您可以使用
layout\u alignParentTop=“true”
将视图放在最上面。然后自由地使用
layout_down=“@+id/id_of_view_to_be_down”
以确保视图始终位于俯视图下方

在代码中,必须与相关属性一起使用,并将其应用于子视图

xml格式的示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
>

<ImageView
    android:id="@+id/img1"
    android:src="@drawable/payarrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"/>

<ImageView
    android:id="@+id/img2"
    android:src="@drawable/chargearrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/img1"
    />
<ImageView
    android:id="@+id/landingDockLeft"
    android:src="@drawable/landingdock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/img2"
    />
<ImageView
    android:id="@+id/landingDockRight"
    android:src="@drawable/landingdock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/landingDockLeft"
    />

这将使它们彼此堆叠在一起,
img1
视图位于整个内容的顶部

编码方式如下。

这是一个示例: 1) 如果你把

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
>

<ImageView
    android:id="@+id/img1"
    />    
<ImageView
    android:id="@+id/img2"
    />

id为img1的ImageView将位于id为img2的ImageView下


2) 如果你想把TextView放在id为img2的ImageView下,你会喜欢的

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    >

    <ImageView
        android:id="@+id/img1"
        />
    <TextView
        android:id="@+id/txt1"
        />        
    <ImageView
        android:id="@+id/img2"
        />


因此,这是订购的问题。

RelativeLayout中的最后一个视图将是最上面的视图Hey Deev,谢谢,但这不是我想要的。我希望这些图像能够层叠在一起。一层在另一层之上。我希望它们重叠。然而,我希望img1在任何情况下都直接位于img3和img4之上。你知道怎么做吗?Layout_alignParentTop和stuff只负责确保图像视图本身存在于一行中。有什么建议吗?除非您另有规定,否则在应用程序的整个生命周期内,视图的z顺序应保持不变。您可以使用诸如
layout\u alignTop
layout\u alignBottom
layout\u alignLeft
layout\u alignBottom
等属性,根据视图的位置将视图与其他视图对齐。我没有试过这个,但我打赌如果你把
android:layout\u alignTop=“@+id/img1”
android:layout\u alignLeft=“@id/img1”
放到
img2
,它会把img2对准img1的左上角。