簡體   English   中英

在 ConstraintLayout 中添加 FrameLayout

[英]Adding FrameLayout Inside ConstraintLayout

我正在嘗試按照文檔添加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);
        }

正如圖書館 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. 在評論中的建議:

您在<RecyclerView>上有app:layout_constraintTop_toBottomOf="@id/toolbar" ,但您的<MaterialToolbar><FrameLayout> 您顯然無法約束到不是<ConstraintLayout>的直接子級的<View> <ConstraintLayout> 也許您打算將其限制在toolbar_layout的底部。

這就是我所缺少的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM