在ConstraintLayout内添加FrameLayout

问题描述 投票:0回答:2

我正在尝试按文档所述添加MaterialSearchView,但是我XML布局中的父布局是ConstraintLayout,并且当我尝试打开它时活动崩溃时,日志显示无法将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>

活动初始化:

 @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);
        }
java android android-xml
2个回答
1
投票

作为库的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>


0
投票

基于@Mike M.在评论中回答“您在上有app:layout_constraintTop_toBottomOf =“ @ id / toolbar”,但您位于内。您显然不能将约束为不是的直接子项。也许您打算将其限制在toolbar_layout的底部。”那是我所缺少的。

© www.soinside.com 2019 - 2024. All rights reserved.