Android Relativelayout,视图不覆盖按钮

时间:2017-05-22 14:52:49

标签: android android-layout relativelayout

我正在尝试使用RelativeLayout实现简单的视图叠加,但似乎有些错误,我希望布局中的最后一个视图在其余部分上呈现,但它实际上是在后面渲染。你能在我的代码中发现什么错误吗?

编辑:弄明白只有当我想用其他视图覆盖按钮时才会发生

enter image description here

这是视图xml:

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">

<Button
    android:layout_alignParentTop="true"
    android:id="@+id/btn1"
    android:text="Normal View"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<FrameLayout
    android:background="#ff0000"
    android:layout_width="match_parent"
        android:layout_height="match_parent"/>

 </RelativeLayout>

1 个答案:

答案 0 :(得分:1)

API 21及更高版本中的

按钮和其他元素具有较高的高程,因此无论父布局如何,都忽略元素的xml顺序。感谢this回答。

此错误的解决方案是在视图上设置更高的高程,我希望在按钮上呈现该高程,覆盖视图的高程值应高于按钮的高程值。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            view.setElevation(1000);
}

或在xml中

<FrameLayout 
    android:elevation="1000"
...
相关问题