在android上的LinearLayout上设置“z-index”

时间:2015-05-07 03:33:36

标签: android android-layout

我在垂直线性布局上放置了四个图像视图。我希望他们能够在相同的空间内进行操作,因此我为每个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>

感谢。

3 个答案:

答案 0 :(得分:6)

如果要反转绘图顺序,则需要继承LinearLayout类并覆盖getChildDrawingOrder。

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

确保在某处启用自定义订购:

setChildrenDrawingOrderEnabled(true);

答案 1 :(得分:2)

对于Android级别的Android,您可以使用view.setZ()http://developer.android.com/reference/android/view/View.html#Drawing

对于21以下的Android级别,我建议使用FrameLayout或RelativeLayout结合使用bringToFront()和/或负填充,如果需要,可以使用保证金。要使用bringToFront()方法,请参阅此Defining Z order of views of RelativeLayout in Android

答案 2 :(得分:2)

为了实现这种布局,FrameLayout将是您最好的选择。

此布局通常用于基于z-Index的结构(重叠)。在这里查看这个课程: - FrameLayout

以下是一个显示其用途的链接: - Example Given.

您也可以找到其他链接来证明其用途。

希望它有所帮助,

感谢。