相对布局在Lollipop版本中的z-order问题

8

我已经在API 19(4.2.2)上测试了一个小的布局,一切正常,但无法在API 21(5.0.1)上运行。

这是该布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/start_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textStyle="bold"
        android:textSize="30sp"
        android:background="@color/start_button_bg"
        android:textColor="@color/start_button_text"
        android:text="@string/start_process"/>

    <TextView
        android:id="@+id/pro_icon"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:textColor="@android:color/white"
        android:background="@drawable/pro_icon"
        android:text="PRO"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium"/>

</RelativeLayout>

非常简单的事情。我只想要在相对布局中,TextView覆盖在Button上面。我知道相对布局按照xml中添加的顺序堆叠视图,但是在棒棒糖上似乎不起作用。我无法让TextView渲染在按钮上方。有什么想法吗?

谢谢。

1个回答

15

Button 在 Android 5.0 及以上版本中默认具有 elevation,而 TextView 则没有。一个控件的 elevation 值会覆盖由 RelativeLayoutFrameLayout 设置的 Z 轴顺序。

你可以选择:

  1. 重新设计UI,不再使用 Z 轴顺序排列组件;或者

  2. 替换 Button 上的 StateListAnimator,以修改或删除 elevation 动画;或者

  3. 尝试通过 android:elevationTextView 的 elevation 值设置得比 Button 更高。


您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Notbad
@Notbad:可能吧,但我不知道具体的技巧。 - CommonsWare

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接