【问题标题】:Android GridLayoutManager one background image for whole rowAndroid GridLayoutManager 一整行的背景图片
【发布时间】:2019-03-03 15:15:47
【问题描述】:

我正在尝试制作简单的布局,但无法正常工作。目标是让活动显示(可配置的)书架上的书籍数量。

我在主要活动中有回收站视图(所以我可以滚动所有这些)和网格布局管理器(所以我可以配置行中的书籍数量)。

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bookshelfRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
</android.support.v7.widget.RecyclerView>

我的问题是,我必须为每一行设置背景图像,但图像用于每本书。就像当我想要一排 3 本书时,我有 3 张 bcgr 图像,而不是一张包裹整行。

<RelativeLayout
 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="wrap_content"
 android:background="@drawable/shelfcell_background">
<ImageView android:id="@+id/imgThumbnail"
            android:layout_width="103dp"
            android:layout_height="134dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_centerInParent="true"
            android:gravity="center_vertical"
            android:layout_marginLeft="20dp"
            tools:ignore="RtlCompat"
            android:layout_marginTop="1dp"/>
</RelativeLayout>

如何设置整行的背景图片(现在不管有多少本书)?

bookshelf - 我在每张图片下都有“新”背景(棕色),但我需要这个背景覆盖整行

【问题讨论】:

  • 你能发一些截图吗?
  • 屏幕即书架

标签: android android-recyclerview grid-layout


【解决方案1】:

这是一种解决方法,您必须将背景(棕色)架子图像切成 3 个相等的部分。

数据中的第一项将具有图像的第一部分,而行的最后一项将具有图像的最后一部分。第二项有中间部分。

这样您可以将书架背景设置为整行,而不仅仅是一个/书。

我希望你明白我想说的话。首先,用一些编辑器或其他东西或油漆剪切背景图像。

假设您连续有 5 个项目。

item 1 - 图片的第 1 部分。 item 2 - 图片的第 2 部分。 item 3 - 图片的第 2 部分。 item 4 - 图片的第 2 部分。 item 5 - 图片的第 3 部分。

【讨论】:

  • 谢谢阿曼 :) 你说得对,这可以解决问题。但另一方面,它看起来太多了。即使结合其他视图/布局,是否还有其他方法?我正在考虑设置背景和动态设置图像数量的线性布局......但我是初学者,所以不确定这是否可行。我试试看。
  • 还有其他方法,但比这复杂得多。这是最简单的方法恕我直言,或者可能是唯一的方法。您可以做一件事,在其上方添加一个垂直回收器视图和您的网格布局管理器。您需要将 RelativeLayout 作为父视图。并将gridlayout项的透明度设置为100%。但是为此,您需要创建一个单独的数据类来了解 recyclerview 项目的确切数量,在您的情况下,totalnumberofgriditem/3 是您的 recyclerview 项目。我希望你能明白。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多