【发布时间】:2020-09-13 02:36:27
【问题描述】:
我想实现这个:
所以我想我可以使用 MaterialShapeDrawable 来应用该转换:
binding.card.background = MaterialShapeDrawable(
ShapeAppearanceModel.builder()
.setAllCornerSizes(5.dpToPx().toFloat())
.setTopEdge(object : TriangleEdgeTreatment(7.dpToPx().toFloat(), false){
override fun getEdgePath(length: Float, center: Float, interpolation: Float, shapePath: ShapePath) {
super.getEdgePath(length, 12.dpToPx().toFloat(), interpolation, shapePath)
}
})
.build()
).apply {
fillColor = resourcesProvider().colorStateListFromAttr(R.attr.colorSurface, R.style.App_CardView)
}
但不幸的是,它没有任何改变。所以我尝试申请这样的直系子女:
<com.google.android.material.card.MaterialCardView
style="@style/Widget.MaterialComponents.CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:clipChildren="false"
app:cardBackgroundColor="@android:color/transparent"
app:cardElevation="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="10dp"> //without this margin nothing is shown
它有点工作,但由于app:cardElevation="10dp"(这是必需的),我得到了一个时髦的结果:
所以我的问题是:
1 - 是否可以对 CardView 应用转换?如果有,怎么做?
2 - 我该如何解决这个“顶部阴影”问题?
谢谢。
【问题讨论】:
-
10dp 的高度太大了,这就是为什么那个时髦的阴影。您必须将高度降低到 3dp 才能获得所需的输出。或者,如果必须使用 10dp,我建议在带有填充的父布局中使用它,给它一个高度,并减少你的卡片的高度。这样你的 cardView 的阴影会减少,但它仍然会被提升那么多。或者,您可以使用Bubble 视图,或者如果您不想使用完整的 Repo,可以在 repo 中搜索气泡视图的代码。我使用它是因为它的阴影看起来比 CardView 的更真实。
标签: android material-design android-cardview android-styles material-components-android