将协调布局中的自定义视图置于前台

4

实际上已经有类似的问题:BringToFront在coordinatorlayout内不起作用

然而,没有建议能够帮助我使bringToFront在coordinatorlayout内起作用。

这是布局XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/toolbar"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"/>

        <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/main_content_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <fragment
                android:id="@+id/navigation_drawer_container"
                android:name="..."
                android:layout_width="@dimen/navigation_drawer_width"
                android:layout_height="match_parent"
                android:layout_gravity="start"/>

        </android.support.v4.widget.DrawerLayout>

    </LinearLayout>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/..."
        android:clickable="true"
        android:layout_gravity="bottom|end"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="60dp"
        app:borderWidth="0dp"/>

    <CustomView
        android:id="@+id/customview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:orientation="vertical"
        android:visibility="gone"
        app:layout_behavior="..."/>

</android.support.design.widget.CoordinatorLayout>

这个自定义视图继承自FrameLayout,可以改变它的高度。

想要将自定义视图放到最前面,我首先尝试添加了以下代码:

mCustomView.bringToFront();

那么

mCustomView.requestLayout();
mCustomView.invalidate();

最后也同样重要。
((View)mCustomView.getParent()).requestLayout();

但是没有成功。按钮仍然在顶部。

我错过了什么吗?

也许还有其他可能将自定义视图置于前端或将按钮放在自定义视图下面?

我还尝试为按钮添加行为,但由于其可见性不仅取决于自定义视图,我放弃了这种方法。

提前感谢您的任何帮助。


我有同样的问题,你最终解决了吗? - David Vávra
@DavidVávra 没有,我已经放弃了。不过,如果你没有使用最新的android支持库,你尝试升级一下吗? - Tima
我通过使用translationZ属性解决了这个问题,它在API 21及以上版本上表现良好。我们稍后会处理兼容性问题。我们正在使用最新的支持库。 - David Vávra
@DavidVávra,我们不能使用它,我们需要支持API 14+。 - Tima
1个回答

2

我终于找到了一个解决方法 - 如果我在AppBar上设置setEnabled(false),它就会进入后台,无论API级别如何,我都可以在其上绘制其他内容。


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