strokeColor和strokeWidth在androidx.cardview.widget.CardView android中不起作用

问题描述 投票:0回答:2

[试图在cardViewandroidx.cardview.widget.CardView)周围设置边框

这里是代码:

<androidx.cardview.widget.CardView

    android:id="@+id/cardView"
    android:layout_width="273dp"
    android:layout_height="118dp"
    android:layout_marginStart="9dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="9dp"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?android:attr/selectableItemBackground"
    android:orientation="vertical"
    app:cardCornerRadius="8dp"

    android:background="@drawable/fix_border" 
    app:strokeColor="#dedede"
    app:strokeWidth="3dp">

</androidx.cardview.widget.CardView>

结果:

border not showing

[也尝试使用自定义形状作为背景(fix_border.xml),也不起作用。

fix_border.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="2dp"
        android:color="#dedede" />
    <!--    <solid android:color="#ffffff" />-->
    <padding
        android:bottom="7dp"
        android:left="7dp"
        android:right="7dp"
        android:top="7dp" />
</shape>
android material-design android-cardview androidx material-components-android
2个回答
0
投票

不可能给CardView提供可绘制的背景(参考:v7 cardview library issues),尽管可以给它提供app:cardBackgroundColor,但是如果需要笔触,可以在其中添加一个布局并添加具有可触性的可绘制背景,并且可以正常工作。因此,类似:

<androidx.cardview.widget.CardView
    android:id="@+id/cardView"
    android:layout_width="273dp"
    android:layout_height="118dp"
    android:layout_marginStart="9dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="9dp"
    android:padding="10dp"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical"
    app:cardCornerRadius="8dp">
    <!-- Any layout type here -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/fix_border">
        <!-- Build your layout, with a background stroke! -->
    </RelativeLayout>
</androidx.cardview.widget.CardView>

0
投票

只需使用Material Components Library和扩展MaterialCardViewandroidx.cardview.widget.CardView。类似于:

    <com.google.android.material.card.MaterialCardView
        app:strokeColor="@color/primaryDarkColor"
        app:strokeWidth="3dp"
        ../>

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.