根据 Android 开发者文档和 Material.io,两者都说我需要同时设置笔划宽度和笔划颜色才能渲染笔划
"可以使用 strokeWidth 属性设置笔划宽度。设置 使用 strokeColor 属性的描边颜色。没有描边颜色, 卡片不会呈现描边边框,无论 strokeWidth 值。”
"注意:如果没有 app:strokeColor,卡片将不会渲染描边 边框,无论 app:strokeWidth 值如何。”
不过,我测试了只需要在我的XML中设置笔划宽度,就可以在材质卡视图中以白色显示笔划。相反,ShapeableImageView 需要设置笔划宽度和颜色才能显示笔划。
知道为什么 MatericalCardView 中的文档和实际代码实现之间存在差异吗?
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="0dp
app:strokeWidth="5dp"> //only this line to show stroke
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:imageUrl='@{imageUrl}'>
</com.google.android.material.imageview.ShapeableImageView>
</com.google.android.material.card.MaterialCardView>
您可以通过为组件分配样式来调整它,如果它不起作用,请检查您的颜色文件。
<com.google.android.material.card.MaterialCardView
style="@style/Widget.Material3.CardView.Outlined"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="0dp"
app:strokeWidth="5dp"
app:strokeColor="?attr/colorPrimary">
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:imageUrl='@{imageUrl}'></com.google.android.material.imageview.ShapeableImageView>
</com.google.android.material.card.MaterialCardView>