【问题标题】:ImageButton and Button in FrameLayout won't change z-indexFrameLayout 中的 ImageButton 和 Button 不会改变 z-index
【发布时间】:2017-11-25 21:18:59
【问题描述】:

切换元素顺序时,将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>

【问题讨论】:

  • FrameLayout 中放置两个按钮有什么意义?
  • 按钮没有ID,怎么调用bringToFront()?贴出代码
  • 重点是将一个 Button 放在一边,有一些偏移,但在另一个之上。
  • 我尝试bringToFront() 他们有ID,这里的重点是我认为他们应该在框架中改变顺序时改变堆栈位置
  • 发布代码,没有它我们可以帮助你......也许你以错误的方式使用了 BringToFront() 或其他什么,现在我们只能相信你的话

标签: android android-layout


【解决方案1】:

我试图重现该问题:它是在 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>

translationZ 在 pre-Lollipop 上不可用这一事实并不重要(并且属性将被忽略),因为不存在这样的问题。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 2014-07-18
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 2015-11-11
    相关资源
    最近更新 更多