android - 如何在 Android 5 中将 ImageView 放在 Button 前面?

标签 android android-5.0-lollipop

在 lolipop 之前的 android 版本中,以下代码有效并且图像位于按钮前面。但是在 android 5 中,imageview 被放在按钮后面。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button"
    android:layout_width="210sp"
    android:layout_height="210sp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/round_button"
    android:drawablePadding="10dip"
    android:gravity="center_vertical|center_horizontal" />


<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/torch"
    android:src="?attr/imageview" />

 </RelativeLayout>

最佳答案

问题出现在 Android 5.0 的 elevation 属性上。显然,RelativeLayout Z 轴顺序与 elevation 相关联。如果两个小部件具有相同的 elevation,则 RelativeLayout 将确定 Z 轴顺序——您可以看到,如果您将布局切换为 例如,按钮 小部件。但是,如果一个小部件 (Button) 有一个 elevation,而另一个小部件 (ImageView) 没有,则 elevation 将优先。

您可以通过 android:stateListAnimator="@null"defining your own custom animator 移除 Button elevation .或者,您可以向 ImageView 添加一些 elevation,使其在 Z 轴上高于 Button

关于android - 如何在 Android 5 中将 ImageView 放在 Button 前面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27216080/

相关文章:

android - 在 AppCompatActivity 的情况下不应用字体样式

android - DisplayMetrics 返回零

android - 撤消Android应用程序上的谷歌帐户访问确认

android - Branch.io:点击链接时深度链接到特定 Activity

java - 发送短信 - 正文未在 Lollipop 中传输

android - 在 api21 上添加 PersistentPreferredActivity

java - 添加到数组时强制关闭

android - 如何在按下按钮时发送单个数据而不在内部循环并在释放按钮时停止操作

java.lang.NoSuchMethodException : getTetherableIfaces on Lollipop 异常

Android PopupWindow 高程不显示阴影