【问题标题】:Setting "z-index" on LinearLayout on android在android上的LinearLayout上设置“z-index”
【发布时间】:2015-05-07 03:33:36
【问题描述】:

我将四个图像视图放置在垂直线性布局上。我希望他们占据相同的空间,所以我为每个人分配一个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>

谢谢。

【问题讨论】:

  • 为每个ImageView提供一个水平方向的LinearLayout
  • 那么我如何控制“z-index”?
  • 对于 z-index,您可以从顶部以负数形式设置边距,例如 marginTop="-20dp"
  • 这就是我目前正在做的事情。我定义的边距顶部/底部是负值。

标签: android android-layout


【解决方案1】:

如果你想颠倒绘制顺序,你需要继承LinearLayout类并重写getChildDrawingOrder。

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

确保在某处启用自定义排序:

setChildrenDrawingOrderEnabled(true);

【讨论】:

    【解决方案2】:

    对于 21 级以上的 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

    【讨论】:

      【解决方案3】:

      对于实现这种布局 FrameLayout 将是您最好的选择。

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

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

      您也可以找到其他链接来展示它的用途。

      希望对你有帮助,

      谢谢。

      【讨论】:

        猜你喜欢
        • 2017-07-06
        • 1970-01-01
        • 2011-07-10
        • 1970-01-01
        • 2014-11-29
        • 1970-01-01
        • 1970-01-01
        • 2012-02-28
        • 1970-01-01
        相关资源
        最近更新 更多