简体   繁体   English

如何制作覆盖 recyclerView 的背景?

[英]How can I make a background overlaying the recyclerView?

I have a horizontal recyclerView of images.我有一个图像的水平 recyclerView。 I want to set a background color that covers the recyclerView.我想设置一个覆盖 recyclerView 的背景颜色。

I want to:我想要:

在此处输入图片说明

I made with my xml code:我用我的 xml 代码: 在此处输入图片说明

I've added a view to the RecyclerView area.我在 RecyclerView 区域添加了一个视图。 Background color gave it there.背景颜色给它那里。 When I write it like this in the code, the background color is under the recyclerView:当我在代码中这样写时,背景颜色在recyclerView下:

gradient background:渐变背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <gradient
        android:angle="0"
        android:centerColor="#f1f1f1"
        android:endColor="#FFF8F8F6"
        android:startColor="@color/white" />

</shape>

xml layout: xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentTop="true"
        android:background="@color/colorAccent" />

    <FrameLayout
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottomView"
        android:layout_margin="5dp"
        android:layout_below="@+id/toolbar"
        android:layout_gravity="bottom" />

        <View
        android:id="@+id/bottomView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignStart="@+id/images"
        android:layout_alignLeft="@+id/images"
        android:layout_alignTop="@+id/images"
        android:layout_alignEnd="@+id/images"
        android:layout_alignRight="@+id/images"
        android:layout_alignBottom="@+id/images"
        android:layout_alignParentBottom="true"
        android:background="@drawable/gradient_white"  />

        <ImageView
            android:id="@+id/sendButton"
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:adjustViewBounds="true"
            android:contentDescription="@null"
            android:elevation="5dp"
            android:gravity="center"
            android:padding="14dp"
            android:scaleType="center"
            app:srcCompat="@drawable/ic_baseline_send_24" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/images"
            android:layout_width="match_parent"
            android:layout_height="110dp"
            android:layout_gravity="bottom"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="10dp" />
</RelativeLayout>

first of all rearrange order of your View s - first should be RecyclerView , then covering background, then send icon.首先重新排列您的View的顺序 - 首先应该是RecyclerView ,然后覆盖背景,然后发送图标。 View s placed in XML are drawn in order of declaration, treat them like layers in your case (and for the future: be aware of translationZ and elevation XML attributes)放置在 XML 中的View按声明的顺序绘制,在您的案例中将它们视为图层(以及将来:注意translationZelevation XML 属性)

and for placing covering "background" View (in fact this is foreground, "above" RecyclerView ) use this:并且为了放置覆盖“背景” View (实际上这是前景,“上面” RecyclerView )使用这个:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/images" ....

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#80FFFFFF"
    android:layout_alignBottom="@id/images"
    android:layout_alignTop="@id/images"
    android:layout_alignLeft="@id/images"
    android:layout_alignRight="@id/images"/>

<ImageView
    android:id="@+id/sendButton" ...

"covering" View may have 0dp size for better measuring performance, still all android:layout_align... declarations make it stretch to the size of RecyclerView (and as later-declared this View will be drawn after RecyclerView , so on top of it) “覆盖” View可能具有0dp大小以更好地测量性能,仍然所有android:layout_align...声明使其拉伸到RecyclerView的大小(并且稍后声明此View将在RecyclerView之后绘制,因此在其之上)

just adjust your android:background , use some gradient drawable with transparency只需调整您的android:background ,使用一些具有透明度的渐变可绘制

edit: gradient drawable file (put in drawable folder)编辑:渐变可绘制文件(放在drawable文件夹中)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
    android:startColor="#00ffffff"
    android:centerColor="#00ffffff"
    android:centerY="80%"
    android:endColor="#fff"
    android:angle="0"
    android:dither="true"/>
</shape>

set it up as将其设置为

android:background="@drawable/your_file_name_of_above_shape"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM