页面内容与 CoordinatorLayout 中的 Toolbar 和 SearchBar 重叠

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

每当我尝试在 Android Studio 中创建带有 SearchBarCoordinatorLayout 时,页面其余部分的内容会与

SearchBar
和我创建的
MaterialToolbar
重叠。

我已经尝试用其他布局替换

CoordinatorLayout
,例如
ConstraintLayout
RelativeLayout
LinearLayout
,但随后
SearchBar
不再按预期扩展为
SearchView
。我知道这是
CoordinatorLayout
的正常行为,但尽管搜索了2天,我还是找不到任何方法来达到正常的外观。

这是我打算发生这种情况的片段的代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".activities.SearchFragment">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/topAppBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:title="Browse" />

        <com.google.android.material.search.SearchBar
            android:id="@+id/search_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Search products" />
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Testing"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.search.SearchView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="Search products"
        app:layout_anchor="@id/search_bar">
<!--            Search suggestions/results go here (ScrollView, RecyclerView, etc.). -->
    </com.google.android.material.search.SearchView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Here's what's happeninghere's what I intend to happen.

我几乎到处搜索,但找不到解决方案。请帮我解决这个问题,请不要举报我,因为我不想被第三次禁止。提前谢谢你!

android material-ui android-coordinatorlayout
© www.soinside.com 2019 - 2024. All rights reserved.