2011-11-18 84 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,因此它是排序的問題。