【问题标题】:CardView in Grid not displaying shadow on all sides网格中的 CardView 未在所有侧面显示阴影
【发布时间】:2018-02-07 06:52:15
【问题描述】:

我最近放弃了为GridView 项目添加阴影的想法并实现了CardViews。

我面临的问题如下:CardView 项目在GridView 的边界处没有显示阴影,如下图所示:

Android Studio 中的CardView Preview 显示了四边都有边框的布局,如this picture 所示。

如何在 GridView 的侧面也添加阴影?为什么Cardview 项目没有影子?

我尝试减少/增加 GridView 列的大小,我尝试将 margin 和 padding 添加到 GridView。我尝试为Cardview 添加更大的边距。他们都没有工作。

这是GridView的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<GridView
    android:id="@+id/CatalogGridList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:horizontalSpacing="8dp"
    android:numColumns="auto_fit"
    android:columnWidth="128dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="8dp"/>
</LinearLayout>

This 是 CardView 布局 xml 的要点。

【问题讨论】:

  • 在 GridView 标签中尝试 android:padding="" 属性
  • @AmeerHamza 正如我在帖子中所说,添加填充会增加 GridView 边距上的空白。没有来自CardView的阴影
  • 尝试转换 android:layout_width="match_parent" 这个 wrap_contant 然后使用 padding
  • @AmeerHamza 我恐怕也不能解决它。

标签: android gridview android-cardview cardview


【解决方案1】:

诀窍是使用android:clipToPadding 属性和水平填充。将这些属性添加到您的 GridView 标记中:

    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:clipToPadding="false"

如果不设置clipToPadding="false",你会看到你提到的白条,但有了它你会看到:

【讨论】:

  • 这是我一直在寻找的答案。谢谢!
【解决方案2】:

正如@Ameer 所说,您需要在包含的GridView 上添加一些填充,以便为阴影留出空间。根据需要调整填充属性(左右):

<GridView
    android:id="@+id/CatalogGridList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:horizontalSpacing="8dp"
    android:numColumns="auto_fit"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:columnWidth="128dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="8dp"/>

【讨论】:

  • 如果我按照您的建议添加填充,并且正如我在帖子中所说,我只会在侧面得到空白。 This is the proof
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-10
  • 2017-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多