本文介绍了FrameLayout 中不正确的叠加行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 docs,FrameLayout 中的子视图绘制在彼此顶部,最近添加的视图在顶部.不过我注意到,这在 Lollipop 或更高版本中似乎不准确.例如,在下面的 xml 中,即使按钮应该被我的自定义视图覆盖,它仍然可见.

According to the docs, child views in FrameLayout are drawn on top of each other with the most recently added view on top. I have notice though, that this seems to not be accurate in Lollipop or later. For example, in the following xml, the button is still visible even though it should be covered by my custom view.

值得一提的是,虽然我的自定义视图继承了 FrameLayout,但我确实膨胀了一个子视图,所以我的 FrameLayout 不是空的.

It is worth mentioning that although my custom view extends FrameLayout, I do inflate a child view, so my FrameLayout isn't empty.

<FrameLayout
    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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/some_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:text="button"/>

    <mycustomframelayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"/>

</FrameLayout>

知道我在这里遗漏了什么吗?

Any idea what I am missing here?

推荐答案

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

Buttons in Lollipop and higher have a default elevation to them which causes them to always draw on top. You can change this by overriding the default StateListAnimator.

尝试将其放入按钮 XML 中:

Try putting this into your button XML:

android:stateListAnimator="@null"

FrameLayout 现在应该覆盖按钮.

The FrameLayout should now cover the button.

这篇关于FrameLayout 中不正确的叠加行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 04:43