FrameLayout中的ImageButton和Button不会更改z-index

时间:2017-02-08 09:00:10

标签: android android-layout

当您切换元素的顺序时,在Button之后放置ImageButton,z-index不会受到影响。我尝试使用其他类型的视图,并根据父级FrameLayout中的顺序将它们正确放置在彼此之上。 我以编程方式尝试使用View.bringToFront()但没有成功。

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Suzi"
        android:textSize="22sp" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />

</FrameLayout>

1 个答案:

答案 0 :(得分:2)

我试图重现这个问题:它是在Lollipop +设备上为我复制的,不会在Lollipop之前复制。 你能检查一下吗?

如果确实如此,那么解决方案可能是使用android:translationZ属性(自Lollipop起可用)

类似的东西:

<FrameLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Suzi"
        android:textSize="22sp"
        android:translationZ="1dp"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:translationZ="2dp"/>

</FrameLayout>

而且翻译Z在前Lollipop上不可用这一事实无关紧要(属性会被忽略),因为没有这样的问题。

希望有所帮助

相关问题