按钮始终显示在FrameLayout的顶部

Nok*_*uap 38 android android-framelayout

我有这样的FrameLayout:

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="changeColor"
        android:text="new button"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="some text"/>

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

问题是按钮显示在顶部,而FrameLayout类概述告诉我们:"子视图是在堆栈中绘制的,最近添加的子项位于顶部".

Rah*_*ari 63

这个答案

Lollipop及更高版本中的按钮具有默认高度,这使得它们始终在顶部绘制.您可以通过覆盖默认的StateListAnimator来更改此设置.

尝试将其放入按钮XML:

android:stateListAnimator="@null"

FrameLayout现在应该覆盖按钮.

  • 是的,这将删除使用高程的动画。您的按钮现在变成一个简单的ImageView。正确的解决方案是将其他项目的高度调整到高于您接受的答案的按钮。 (3认同)
  • 你救了我的一天。这种事情应该更好地宣传。 (2认同)

Kho*_* Vo 15

在Android 5.0(API 21)及更高版本中,您必须在视图中添加android:elevation.

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="changeColor"
        android:text="new button"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="some text"
        android:elevation="3dp"/>
Run Code Online (Sandbox Code Playgroud)