为了账号安全,请及时绑定邮箱和手机立即绑定

RecyclerView 之上的透明 TextView

RecyclerView 之上的透明 TextView

万千封印 2024-01-05 10:06:06
我有一个活动,其中有一个 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>
查看完整描述

2 回答

?
慕村9548890

TA贡献1884条经验 获得超4个赞

用于NestedScrollView使整个视图可滚动


<?xml version="1.0" encoding="utf-8"?>

<androidx.core.widget.NestedScrollView 

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:fillViewport="true"

    tools:context=".Activity"

    android:layout_width="match_parent"

    android:layout_height="wrap_content">

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical"

        android:background="@drawable/gradient_background">


        <RelativeLayout

            android:id="@+id/relativeLayout"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            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"/>


    </LinearLayout>

</androidx.core.widget.NestedScrollView>


查看完整回答
反对 回复 2024-01-05
?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

我可以让RelativeLayout不透明,以便RecyclerView项目滚动到它的“后面”吗?

只需将约束布局更改为线性布局即可。

我如何才能使整个布局与 RecyclerView 一起滚动?

逻辑:为此,您必须在要扩充 RecyclerView 内容的 XML 文件中包含相对布局。现在,每次都会正确地膨胀相对布局。

因此,为了克服这个问题,无论您在 RecyclerView 构造函数中传递什么,都需要一个标志/计数,仅当计数等于零/设置标志时以及当计数大于 0 或设置标志时才膨胀相对布局,然后隐藏相对布局。

这样,相对布局就会随 RecyclerView 内容一起滚动,并且仅膨胀一次。


查看完整回答
反对 回复 2024-01-05
  • 2 回答
  • 0 关注
  • 47 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信