2011-11-18 131 views
0

在一个相关图层中,基于您的xml文件将图像级联在另一个图层上。你如何指出最上面的图像应该是什么?Android - RelativeLayout问题

有没有办法在代码而不是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" 
    /> 

+0

“RelativeLayout”中的最后一个视图将成为最顶层的视图 – binnyb

回答

0

机器人:layout_alignParentTop = “真”

1

在XML中,你可以使用layout_alignParentTop="true"把视图中的最高层。然后自由使用layout_below="@+id/id_of_view_to_be_below"以确保视图始终位于顶部视图之下。

在代码中,您必须使用RelativeLayout.LayoutParams以及相关属性并将它们应用于子视图。

例中的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视图的顶部。

编码方式为here

+0

Hey Deev,谢谢,但这不是我想要的。我希望图像层叠在一起。一层在另一层之上。我希望它们重叠。但是,无论如何,我都希望img1直接位于img3和img4之上。你知道这可以做到吗? Layout_alignParentTop和东西只负责确保图像视图本身存在于一行中。有什么建议么? – roro

+0

除非另有说明,否则视图的z顺序应该在应用程序的整个生命周期中保持不变。您可以使用'layout_alignTop','layout_alignBottom','layout_alignLeft'和'layout_alignBottom'等属性根据视图的位置将视图与其他视图对齐。我还没有尝试过,但我敢打赌,如果你把'android:layout_alignTop =“@ + id/img1”'和'android:layout_alignLeft =“@ id/img1”'到'img2',它会将img2对齐到img1的左上角。 – DeeV

0

这是例子: 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" 
    /> 

ImageView的ID为IMG1将根据与ID IMG2 ImageView的。


2),如果你想要把TextView的,这将是下的ImageView与IMG2你像这样

<?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" 
     /> 

ID,因此它是排序的问题。