【问题标题】:Adding border to Cardview which contains an ImageView向包含 ImageView 的 Cardview 添加边框
【发布时间】:2021-11-26 06:59:24
【问题描述】:

我想为包含 ImageView 的卡片视图添加边框。如果我使用图标作为图像(矢量图形),它会非常完美。图像位于大小为 MATCH_PARENT 的 ImageView 中,并添加到卡片视图中。 cardview应该负责边界。因此,我以这种方式设置了卡片视图的背景图像:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fff"/>
<stroke android:width="1px" android:color="#e6e6e6" />
<corners android:radius="20dp"/>
<padding android:left="0dp" android:top="0dp"
    android:right="0dp" android:bottom="0dp" />
</shape>

所以 ImageView 的图像似乎覆盖了 cardview 的背景。知道如何解决吗?

【问题讨论】:

    标签: android background imageview cardview


    【解决方案1】:

    这不是使用drawable 的直接解决方案,但我建议使用MaterialCardView,它为您提供与使用app:strokeColorapp:strokeWidthapp:cardCornerRadius 的drawable 相同的规格:

    <com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="20dp"
        app:strokeColor="#e6e6e6"
        app:strokeWidth="1dp">
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="@drawable/my_image" />
    </com.google.android.material.card.MaterialCardView>
    

    注意:添加到ImageView的边距等于卡片笔划,因此它不会被卡片笔划覆盖,如果您愿意,可以将其删除。

    【讨论】:

      猜你喜欢
      • 2018-01-02
      • 1970-01-01
      • 2016-05-24
      • 2016-03-20
      • 2018-12-22
      • 1970-01-01
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多