RecyclerView 之上的透明 TextView

我有一个活动,其中有一个 RecyclerView,但在它上面有一个 TextView,现在当我在 RecyclerView 中滚动时,RelativeLayout 中的文本保留在屏幕顶部,并且不会随之滚动。现在我的问题是,RelativeLayout 视图是透明的,因此当我在 RecyclerView 中滚动时,RelativeLayout 中的文本将位于 RecyclerView 中文本元素列表的顶部,换句话说,为了简化我的意思,我有“文本”文本”如果有意义的话。我怎样才能让RelativeLayout不透明,以便RecyclerView项目在它的“后面”滚动并且在TextView的“下面”不可见,或者我如何才能使整个布局与RecyclerView一起滚动?

(即使我更改RelativeLayout的背景颜色,RecyclerView中的文本也将通过它可见)

以下是此活动的 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=".Activity"

    android:background="@drawable/gradient_background">


    <RelativeLayout

        android:id="@+id/relativeLayout"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        app:layout_constraintTop_toTopOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        android:background="@color/colorPrimary">


        <View

            android:id="@+id/lineView"

            android:layout_width="30dp"

            android:layout_height="0.5dp"

            android:background="@drawable/divider_line"

            android:layout_centerVertical="true"/>


        <TextView

            android:id="@+id/textView"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:textSize="24sp"

            android:layout_toEndOf="@id/lineView"

            android:layout_margin="8dp"/>


    </RelativeLayout>


    <androidx.recyclerview.widget.RecyclerView

        android:id="@+id/recyclerView"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_margin="16dp"

        app:layout_constraintTop_toBottomOf="@+id/relativeLayout"/>


</androidx.constraintlayout.widget.ConstraintLayout>


万千封印
浏览 53回答 2
2回答

慕村9548890

用于NestedScrollView使整个视图可滚动<?xml version="1.0" encoding="utf-8"?><androidx.core.widget.NestedScrollView&nbsp;&nbsp; &nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:fillViewport="true"&nbsp; &nbsp; tools:context=".Activity"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="wrap_content">&nbsp; &nbsp; <LinearLayout&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:orientation="vertical"&nbsp; &nbsp; &nbsp; &nbsp; android:background="@drawable/gradient_background">&nbsp; &nbsp; &nbsp; &nbsp; <RelativeLayout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/relativeLayout"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="@color/colorPrimary">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <View&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/lineView"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="30dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="0.5dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="@drawable/divider_line"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_centerVertical="true"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/textView"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textSize="24sp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_toEndOf="@id/lineView"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_margin="8dp"/>&nbsp; &nbsp; &nbsp; &nbsp; </RelativeLayout>&nbsp; &nbsp; &nbsp; &nbsp; <androidx.recyclerview.widget.RecyclerView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/recyclerView"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_margin="16dp"/>&nbsp; &nbsp; </LinearLayout></androidx.core.widget.NestedScrollView>

江户川乱折腾

我可以让RelativeLayout不透明,以便RecyclerView项目滚动到它的“后面”吗?只需将约束布局更改为线性布局即可。我如何才能使整个布局与 RecyclerView 一起滚动?逻辑:为此,您必须在要扩充 RecyclerView 内容的 XML 文件中包含相对布局。现在,每次都会正确地膨胀相对布局。因此,为了克服这个问题,无论您在 RecyclerView 构造函数中传递什么,都需要一个标志/计数,仅当计数等于零/设置标志时以及当计数大于 0 或设置标志时才膨胀相对布局,然后隐藏相对布局。这样,相对布局就会随 RecyclerView 内容一起滚动,并且仅膨胀一次。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java