放置/重叠(z-index)视图在android中的另一个视图上方

sat*_*sat 220 android z-index android-layout

我有一个线性布局,包括imageview和textview,一个在线性布局下面.

<LinearLayout android:orientation="horizontal" ... >
 <ImageView 
     android:id="@+id/thumbnail"
     android:layout_weight="0.8" 
     android:layout_width="0dip"
     android:layout_height="fill_parent">
 </ImageView>
 <TextView 
    android:id="@+id/description"
    android:layout_weight="0.2"
    android:layout_width="0dip"
    android:layout_height="wrap_content">
 </TextView>
Run Code Online (Sandbox Code Playgroud)

可能缺少一些规则,这是为了给出一个想法,布局看起来如何.我希望另一个小文本视图说50dip的长度和宽度,放在imageview上,"over"我的意思是z-index比imageview更多,我想把它放在imageview的中心和上面(重叠).

我想知道如何将一个视图放在另一个视图上方,具有不同的z-index(最好是线性布局)?

kco*_*ock 286

你不能使用LinearLayout,但你可以使用FrameLayout.在a中FrameLayout,z-index由添加项的顺序定义,例如:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/my_drawable"
        android:scaleType="fitCenter"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:padding="5dp"
        android:text="My Label"
        />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,TextView将沿着图像的底部中心绘制在ImageView的顶部.

  • FrameLayout与组中的其他视图无关,它只是根据父级对它们进行分层.RelativeLayout更适用于相对于其他项目的定位. (12认同)
  • 为此,使用FrameLayout而不是RelativeLayout是否有优势? (11认同)
  • 嗨kcoppock我的手机(hdpi)不尊重框架布局中的z顺序,但我有一个其他(xhdpi)并尊重这个顺序,你知道为什么这样做?提前致谢!:d (3认同)
  • 从 API 21 / KitKat 开始,您现在可以使用 setZ 和 TranslationZ;不再需要 FrameLayout hack(终于!)。这应该是现代 5.0+ 开发的首选答案:http://stackoverflow.com/a/29703860/358578 (2认同)

diy*_*ism 181

尝试.bringToFront():

http://developer.android.com/reference/android/view/View.html#bringToFront%28%29

  • bringChildToFront()只是更改其父级内View的索引 (9认同)
  • 我们也可以在xml文件中使用它吗? (3认同)
  • 这会拧紧整个布局顺序 (3认同)
  • 我正在寻找一些能让我的观点在翻译动画中改变itz zindex的东西. (2认同)

jt-*_*son 64

RelativeLayout以相同的方式工作,相对布局中的最后一个图像获胜.

  • 除非您使用高程,否则您还需要最大值. (13认同)
  • 在提升之前,问了这个问题并得到了回答.如果您的应用的minSdk小于Lollipop,则使用提升将无法解决问题(如果您只依赖于提升,则在预制棒棒糖手机上布局会出现错误) - 您仍需要注意布局的顺序.你是对的,如果你在底部组件上使用高程 - 你肯定需要在顶部至少相同或更高. (4认同)

Tob*_*run 26

更改绘制顺序

另一种方法是更改​​父级绘制视图的顺序.您可以通过调用setChildrenDrawingOrderEnabled(true)并覆盖getChildDrawingOrder(int childCount,int i)从ViewGroup启用此功能.

例:

/**
 * Example Layout that changes draw order of a FrameLayout
 */
public class OrderLayout extends FrameLayout {

    private static final int[][] DRAW_ORDERS = new int[][]{
            {0, 1, 2},
            {2, 1, 0},
            {1, 2, 0}
    };

    private int currentOrder;

    public OrderLayout(Context context) {
        super(context);
        setChildrenDrawingOrderEnabled(true);
    }

    public void setDrawOrder(int order) {
        currentOrder = order;
        invalidate();
    }

    @Override
    protected int getChildDrawingOrder(int childCount, int i) {
        return DRAW_ORDERS[currentOrder][i];
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

OrderLayout#setDrawOrder(int)使用0-1-2 调用会导致:

在此输入图像描述 在此输入图像描述 在此输入图像描述


Vad*_*tov 18

您可以view.setZ(float)从API级别21开始使用.您可以在此处找到更多信息.

  • 这应该是API 21/KitKat/5.0或更高版本的首选答案.谢天谢地,不再需要旧的frameLayout hack.就个人而言,我不明白setZ和translationZ之间的区别,但后者是一个属性,对我来说很漂亮.谢谢! (3认同)

thi*_*uan 14

我通过添加android:elevation="1dp"你想要的视图来解决同一个问题.但它不能显示在5.0以下,它会有一点阴影,如果你可以接受它,它没关系.

所以,最正确的解决方案是@kcoppock说.


小智 7

在RelativeLayout中尝试这个:

ImageView image = new ImageView(this);
image.SetZ(float z);
Run Code Online (Sandbox Code Playgroud)

这个对我有用.

  • 您应该更好地解释。例如,您将此代码放在哪里? (2认同)

Tim*_*Tim 5

有一种方法可以使用LinearLayout.只需将前一个元素的marginTop设置为相应的负值,并确保您想要的元素位于XML下面所需的元素之后.

<linearLayout android:orientation="horizontal" ... >
<ImageView 
 android:id="@+id/thumbnail"
 android:layout_weight="0.8" 
 android:layout_width="0dip"
 android:layout_height="fill_parent"
>
</ImageView>
<TextView 
android:id="@+id/description"
android:layout_marginTop="-20dip"
android:layout_weight="0.2"
android:layout_width="0dip"
android:layout_height="wrap_content"
>
</TextView>
Run Code Online (Sandbox Code Playgroud)