2015-05-07 113 views
3

我在垂直線性佈局上放置四個圖像視圖。我希望他們有足夠的空間,所以我分配給每個android:layout_weight="1"。我也希望他們重疊(這是一個設計requeriment),所以我爲每個視圖設置一個負邊距。我添加的最後一張圖像(@+id/floor_1st)是最後添加的圖像(底部的圖像),所以它保留在前面。但是,我希望它是另一種方式:我希望佈局中的第一個圖像位於前面,然後是第二個,等等(最後一個圖像位於後面)。在LinearLayout上設置「z-index」android

據我所知,使用RelativeLayout來控制圖像放置的順序比較容易,但我不知道如何按照我想要的方式使用此佈局來放置圖像。我也看到有可能使用方法bringToFront(),但這只是不讓圖像重疊。

那麼,有什麼辦法可以按照我想要的順序使用LinearLayout來放置圖像嗎?或者我應該使用另一種佈局?在這種情況下,我應該如何放置圖像?

這裏是我的XML代碼

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/floors_container" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="@dimen/overview_buttons_top_margin" 
    android:layout_marginBottom="@dimen/overview_buttons_bottom_margin" 
    > 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/floor_4th" 
     android:src="@drawable/piso_4" 
     android:layout_weight="1" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginBottom="@dimen/floors_overview_margin_three_quarter" 
     android:clickable="true" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/floor_3rd" 
     android:src="@drawable/piso_3" 
     android:layout_gravity="center_horizontal" 
     android:layout_weight="1" 
     android:layout_marginTop="@dimen/floors_overview_margin_quarter" 
     android:layout_marginBottom="@dimen/floors_overview_margin_half" 
     android:clickable="true" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/floor_2nd" 
     android:layout_gravity="center_horizontal" 
     android:src="@drawable/piso_2" 
     android:layout_weight="1" 
     android:layout_marginTop="@dimen/floors_overview_margin_half" 
     android:layout_marginBottom="@dimen/floors_overview_margin_quarter" 
     android:clickable="true" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/floor_1st" 
     android:src="@drawable/piso_1" 
     android:layout_gravity="center_horizontal" 
     android:layout_weight="1" 
     android:layout_marginTop="@dimen/floors_overview_margin_three_quarter" 
     android:clickable="true" /> 
</LinearLayout> 

感謝。

+0

與水平方向提供一個'LinearLayout'每個'ImageView' –

+0

然後我如何控制 「的z-index」? –

+0

對於z-index,您可以從負值形式的頂部設置保證金,例如'marginTop =「 - 20dp」' –

回答

2

爲了實現這種佈局FrameLayout將是你最好的選擇。

此佈局通常用於基於z-Index的結構(重疊)。請在這裏看看這個班級: - FrameLayout

這裏是一個鏈接,顯示了它的用途: - Example Given.

你可以找到其他環節也展示了其使用。

希望它有幫助,

謝謝。

6

如果您想要反轉繪製順序,則需要繼承LinearLayout類並覆蓋getChildDrawingOrder。

@Override 
protected int getChildDrawingOrder(int childCount, int i) { 
    //The standard implementation just retuns i 
    return childCount - i - 1; 
} 

確保啓用自定義排序的話:

setChildrenDrawingOrderEnabled(true); 
+0

該解決方案無法正常工作 –

相關問題