[英]Setting “z-index” on LinearLayout on android
我在垂直線性布局上放置了四個圖像視圖。 我希望他們能夠在同一個空間中占據一席之地,所以我為每個人分配一個android:layout_weight="1"
。 我也希望它們重疊(這是一個設計要求),所以我為每個視圖設置了一個負余量。 我添加的最后一個圖像( @+id/floor_1st
)是最后添加的圖像(底部的圖像),因此它保留在前面。 但是,我希望它是另一種方式:我希望布局上的第一個圖像位於前面,然后是第二個,依此類推(最后一個圖像位於后面)。
我知道使用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>
謝謝。
如果要反轉繪制順序,則需要子類化LinearLayout類並覆蓋getChildDrawingOrder。
@Override
protected int getChildDrawingOrder(int childCount, int i) {
//The standard implementation just retuns i
return childCount - i - 1;
}
確保在某處啟用自定義訂購:
setChildrenDrawingOrderEnabled(true);
對於21級以上的Android,您可以使用view.setZ() http://developer.android.com/reference/android/view/View.html#Drawing
對於低於21的Android級別,我建議使用FrameLayout或RelativeLayout結合使用bringToFront()和/或負填充,如果需要,可以使用保證金。 有關使用bringToFront()方法的信息,請參閱Android中RelativeLayout的定義Z視圖順序
為了實現這種布局,FrameLayout將是您最好的選擇。
此布局通常用於基於z索引的結構(重疊)。 在這里看看這個課程: - FrameLayout 。
這里有一個顯示其用途的鏈接: - 給出示例。
您還可以找到其他鏈接,以證明其用途。
希望能幫助到你,
謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.