Recylerview应该与背景重叠

时间:2018-09-04 05:43:13

标签: android android-layout android-recyclerview

嗨,我想像下面的图像那样设计,前面是带有网格的recylerview,后面是另一个背景。如何创建这种布局。目前我正在使用下面的代码来实现这种设计,但据我所知对于不同的屏幕尺寸,这不是很好的做法。还有另一种方法可以做到这一点。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="70dp"
            android:layout_marginTop="40dp"
            android:background="@color/white" />


    <include
        layout="@layout/recyclerview_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true" />


</RelativeLayout>

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以从布局xml或活动源代码中为RecyclerView设置背景。

<androidx.recyclerview.widget.RecyclerView
   android:id="@+id/recyclerView"
   android:background="@drawable/sample_drawable"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_marginBottom="8dp"
   android:layout_marginTop="8dp" />

recyclerView.background = ContextCompat.getDrawable(this,R.drawable.sample_drawable)

答案 1 :(得分:0)

像这样设计您的父布局:

<android.support.constraint.ConstraintLayout 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"
android:background="@android:color/white">

<!-- Main background which is to be replaced with your image-->
<ImageView
    android:id="@+id/main_background"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:background="@android:color/holo_blue_light"/>

<!-- Sub view with white background -->
<View
    android:id="@+id/sub_background"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="60dp"
    android:layout_marginBottom="60dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:background="@android:color/white"/>

<!-- Background as Transparent-->
<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@android:color/transparent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

还可以使子项目的背景透明。

希望这会有所帮助。