Android - ImageView 的 SearchView 顶部未显示 API > 21

Android - SearchView top of ImageView not showing for API > 21

<?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=".MainActivity">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent="0.4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:elevation="5dp"
        android:translationZ="5dp">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="Book"
            android:src="@drawable/bookbackground"
            android:layout_centerInParent="true"
            android:scaleType="centerCrop"
            />
        <SearchView
            android:id="@+id/books"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Search Books"
            android:layout_marginRight="30dp"
            android:layout_marginLeft="30dp"
            android:elevation="5dp"
            android:translationZ="5dp"
            android:layout_centerInParent="true"

            />

    </RelativeLayout>




<!--https://pixabay.com/photos/books-students-library-university-1281581/ -->
</androidx.constraintlayout.widget.ConstraintLayout>

我认为您犯了一点错误,您应该在图像之前定义搜索视图。

能否从 SearchView 中删除 CenterInParent

android:layout_centerInParent="真"

尝试将背景设置为搜索视图,您一定会看到您的搜索视图... 或使用自定义搜索视图,以便可以看到

<?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=".MainActivity">

<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_default="percent"
    app:layout_constraintHeight_percent="0.4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:elevation="5dp"
    android:translationZ="5dp">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="Book"
        android:src="@drawable/bookbackground"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop"
        />
    <SearchView
        android:id="@+id/books"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Search Books"
        android:layout_marginRight="30dp"
        android:layout_marginLeft="30dp"
        android:elevation="5dp"
        android:translationZ="5dp"
        android:background:"your color"
        android:layout_centerInParent="true"

        />

</RelativeLayout>




  <!--https://pixabay.com/photos/books-students-library-university-1281581/ -->
    </androidx.constraintlayout.widget.ConstraintLayout>