材料景观和顶部的角落应夹住儿童
材料cardview
使用圆角时将其孩子剪辑。如果我使用cardCornerradius = true
(将所有4个角圆形圆角),则剪辑的行为符合预期 - >没有在圆形区域外吸引儿童。如果我尝试更小的角(即只有shapeappearancemodel
>的顶部角落),那么剪辑部分就会丢失 - >将在圆角区域上绘制一个顶级孩子)。
我意识到剪辑MaterialCardView调用 setCliptoOutline(shapeappearancemodel.isroundrect(getBoundSasRectf()));
set setShapeAppeApeModel
的内部,其中isROUNDRECT
仅在所有四个角落都被四舍五入时返回true,因此我尝试在code>上应用
在设置cliptooutline = true
shapeappearancemodel
带有圆角的shapeappearancemodel ,但没有类似的结果 - 仍然可以在父卡的圆形部分上绘制孩子。
什么实际触发了剪裁部分,我该如何将其强加于顶部的圆形材料贴图?
LE:反复试验:
// card is MaterialCardView
card.shapeAppearanceModel =
ShapeAppearanceModel()
.toBuilder()
.setTopRightCorner(CornerFamily.ROUNDED, cornerPx) // cornerPx = 24dp in pixels
.setTopLeftCorner(CornerFamily.ROUNDED, cornerPx)
.build()
card.apply {
preventCornerOverlap = true
clipChildren = true
clipToOutline = true
}
card.invalidateOutline()
MaterialCardView
clips its children when using rounded corners. If I use cardCornerRadius = true
(which rounds all 4 corners of the card), the clipping behaves as expected -> children are not being drawn outside of the rounded area. If I try to round less corners (ie. only top corners, using ShapeAppearanceModel
), then the clipping part is lost -> a top positioned child will be drawn over the rounded cornered area).
I realised that clip MaterialCardView callssetClipToOutline(shapeAppearanceModel.isRoundRect(getBoundsAsRectF()));
inside of setShapeAppearanceModel
, where isRoundRect
returns true only if all four corners are rounded, so I tried applying clipToOutline = true
on the MaterialCardView
after setting the shapeAppearanceModel
with top corners rounded, but with no similar result - children were still able to be drawn over the rounded part of the parent card.
What is actually triggering the clipping part and how can I force it on a top rounded shaped MaterialCardView?
LE: trial and error code:
// card is MaterialCardView
card.shapeAppearanceModel =
ShapeAppearanceModel()
.toBuilder()
.setTopRightCorner(CornerFamily.ROUNDED, cornerPx) // cornerPx = 24dp in pixels
.setTopLeftCorner(CornerFamily.ROUNDED, cornerPx)
.build()
card.apply {
preventCornerOverlap = true
clipChildren = true
clipToOutline = true
}
card.invalidateOutline()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在调试材料浏览量源代码后,我提出了以下子类,这些子类将阻止额外的偏移到填充到填充物,并脱离轮廓。
After debugging the MaterialCardView source code I came up with the following sub-class that will prevent from additional offset to padding and unclipping to outlines.
应用以下配置,如果您使用形状外观覆盖层以防止孩子从角落重叠:
cardpreventcorneroverlap
true然后,您应该看到这样的东西:
Apply the following configurationsif you use shape appearance overlay in order to prevent the childs to overlapping from the corners:
cardPreventCornerOverlap
to trueThen you should see something like this: