[英]Different corner radius values for a MaterialCardView
Is is possible to have different values for each corner radius of a MaterialCardView? MaterialCardView 的每个角半径是否可以有不同的值? And if so how?
如果是这样怎么办?
I tried something like the code below but it doesn't seem to have any effect我尝试了类似下面的代码,但似乎没有任何效果
float radius = getContext().getResources().getDimension(R.dimen.default_corner_radius);
ShapePathModel leftShapePathModel = new ShapePathModel();
leftShapePathModel.setTopLeftCorner(new RoundedCornerTreatment(radius));
leftShapePathModel.setTopRightCorner(new RoundedCornerTreatment(radius));
MaterialShapeDrawable bg = new MaterialShapeDrawable(leftShapePathModel);
container.setBackground(bg);
where container is容器在哪里
@BindView(R.id.container) MaterialCardView container;
You can use a custom style and the shapeAppearanceOverlay
attribute.您可以使用自定义样式和
shapeAppearanceOverlay
属性。
<style name="MyCardView" parent="@style/Widget.MaterialComponents.CardView">
<item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MaterialCardView.Cut</item>
</style>
<style name="ShapeAppearanceOverlay.MaterialCardView.Cut" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">8dp</item>
<item name="cornerSizeTopLeft">8dp</item>
<item name="cornerSizeBottomRight">0dp</item>
<item name="cornerSizeBottomLeft">0dp</item>
</style>
or you can apply a custom ShapeAppearanceModel
to the corner of the card using something like:或者您可以使用以下内容将自定义
ShapeAppearanceModel
到卡片的角落:
float radius = getResources().getDimension(R.dimen.my_corner_radius);
cardView.setShapeAppearanceModel(
cardView.getShapeAppearanceModel()
.toBuilder()
.setTopLeftCorner(CornerFamily.ROUNDED,radius)
.setTopRightCorner(CornerFamily.ROUNDED,radius)
.setBottomRightCornerSize(0)
.setBottomLeftCornerSize(0)
.build());
Try This Also也试试这个
<style name="TopCornerCardview" parent="Widget.MaterialComponents.CardView">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">@dimen/dp25</item>
<item name="cornerSizeTopLeft">@dimen/dp25</item>
<item name="cornerSizeBottomRight">0dp</item>
<item name="cornerSizeBottomLeft">0dp</item>
<item name="contentPadding">0dp</item>
</style>
<com.google.android.material.card.MaterialCardView
style="@style/TopCornerCardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/TopCornerCardview"
app:cardBackgroundColor="@color/colorAccent"
app:cardUseCompatPadding="true">
>
<!--views here-->
</com.google.android.material.card.MaterialCardView>
I think you should be able to call ShapeAppearanceModel shape = ((MaterialShapeDrawable)container.getBackground()).getShapeAppearanceModel()
on your MaterialCardView
.我想你应该能够调用
ShapeAppearanceModel shape = ((MaterialShapeDrawable)container.getBackground()).getShapeAppearanceModel()
在你的MaterialCardView
。 From there you could call setTopLeftCorner()
or the other methods to set corner treatments with different values.从那里您可以调用
setTopLeftCorner()
或其他方法来设置具有不同值的角处理。 You may need to call container.invalidate()
after you set the corners.设置角后,您可能需要调用
container.invalidate()
。
My initial solution was correct but it was missing one line:我最初的解决方案是正确的,但缺少一行:
float radius = getContext().getResources().getDimension(R.dimen.default_corner_radius);
ShapePathModel leftShapePathModel = new ShapePathModel();
leftShapePathModel.setTopLeftCorner(new RoundedCornerTreatment(radius));
leftShapePathModel.setTopRightCorner(new RoundedCornerTreatment(radius));
MaterialShapeDrawable bg = new MaterialShapeDrawable(leftShapePathModel);
container.setBackground(bg);
If you add如果添加
container.invalidate()
as suggested by Cameron above it seems to work.正如上面卡梅伦所建议的那样,它似乎有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.