在 ConstraintLayout 中添加 FrameLayout

Adding FrameLayout Inside ConstraintLayout

我正在尝试按照文档添加 MaterialSearchView,但是我的 XML 布局中的父布局是 ConstraintLayout,当 activity 在我尝试打开它时崩溃,日志显示无法将 FrameLayoutParam 转换为 ConstraintLayoutParam。

这是堆栈跟踪:

java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to androidx.constraintlayout.widget.ConstraintLayout$LayoutParams
    at androidx.constraintlayout.widget.ConstraintLayout.getTargetWidget(ConstraintLayout.java:1144)
    at androidx.constraintlayout.widget.ConstraintLayout.setChildrenConstraints(ConstraintLayout.java:1028)
    at androidx.constraintlayout.widget.ConstraintLayout.updateHierarchy(ConstraintLayout.java:803)
    at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1561)
    at android.view.View.measure(View.java:24545)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)

这是我的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="main.activities.SalesHistoryActivity">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <FrameLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:background="@color/colorPrimary"
            android:elevation="8dp"
            android:theme="@style/CashierToolbarStyle"
            app:popupTheme="@style/ThemeOverlay.MaterialComponents.Toolbar.Primary"
            app:titleTextColor="@color/white" />

        <com.miguelcatalan.materialsearchview.MaterialSearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"/>
    </FrameLayout>
</com.google.android.material.appbar.AppBarLayout>


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/sales_history_list"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/toolbar" />
    </androidx.constraintlayout.widget.ConstraintLayout>

Activity 初始化:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_orders_history);
    butterKnife = ButterKnife.bind(this);
    toolbar.setTitle(R.string.orders_history_title);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    searchView.setQuery("ksks", false);
    searchView.setOnQueryTextListener(this);
        }

作为图书馆 github 的示例应用建议:

  1. 布局父级必须是 FrameLayout
  2. MaterialSearchView 必须位于 XML 文件的末尾才能正常运行。

所以根据样本:

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

    <!-- Your content needs marginTop -->
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize" />

    <!-- Must be last for right layering display -->
    <FrameLayout
        android:id="@+id/toolbar_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/theme_primary" />

        <com.miguelcatalan.materialsearchview.MaterialSearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </FrameLayout>

</FrameLayout>

根据@MikeM.在评论中的建议:

You have app:layout_constraintTop_toBottomOf="@id/toolbar" on the <RecyclerView>, but your <MaterialToolbar> is inside the <FrameLayout>. You apparently can't constrain to a <View> that is not a direct child of the <ConstraintLayout>. Perhaps you meant to constrain it to the bottom of toolbar_layout instead.

这就是我所缺少的。