亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 文件中包含相對布局?,F在,每次都會正確地膨脹相對布局。

因此,為了克服這個問題,無論您在 RecyclerView 構造函數中傳遞什么,都需要一個標志/計數,僅當計數等于零/設置標志時以及當計數大于 0 或設置標志時才膨脹相對布局,然后隱藏相對布局。

這樣,相對布局就會隨 RecyclerView 內容一起滾動,并且僅膨脹一次。


查看完整回答
反對 回復 2024-01-05
  • 2 回答
  • 0 關注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號