2

我需要创建带有两个布局标题和描述的 xml。在标题布局中,我需要添加边框在左下角和右下角有一半的圆圈,并且描述布局边框在左上角和右上方有一半的圆圈。这是我的设计

在此处输入图像描述

我可以通过在矩形半径边界线上创建两个半圆来做到这一点,但我不想使用它。我怎么能用另一种解决方案来做到这一点?请给我关键工作或解决方案!太感谢了!

4

1 回答 1

2

您可以使用ShapeAppearanceModel定义自定义CornerTreatment来应用到组件。就像是:

    val radius = resources.getDimension(R.dimen.default_corner_radius)
    val title_layout = findViewById<LinearLayout>(R.id.title_layout)
    
    val titleShapeModel = ShapeAppearanceModel().toBuilder()
            .setTopLeftCorner(CornerFamily.ROUNDED, radius)
            .setTopRightCorner(CornerFamily.ROUNDED, radius)
            .setBottomLeftCorner(ConcaveRoundedCornerTreatment()).setBottomLeftCornerSize(radius)
            .setBottomRightCorner(ConcaveRoundedCornerTreatment()).setBottomRightCornerSize(radius)
            .build()
    val titleBackground = MaterialShapeDrawable(titleShapeModel)
    titleBackground.setStroke(1f, ContextCompat.getColor(this, R.color.colorPrimaryDark))

    ViewCompat.setBackground(title_layout, titleBackground)

在哪里ConcaveRoundedCornerTreatment

class ConcaveRoundedCornerTreatment : CornerTreatment() {

    override fun getCornerPath(
            shapePath: ShapePath,
            angle: Float,
            interpolation: Float,
            radius: Float
    ) {
        val interpolatedRadius = radius * interpolation
        shapePath.reset(0f, interpolatedRadius, ANGLE_LEFT, ANGLE_LEFT - angle)
        shapePath.addArc(
                -interpolatedRadius,
                -interpolatedRadius,
                interpolatedRadius,
                interpolatedRadius,
                ANGLE_BOTTOM,
                -angle
        )
    }

    companion object {
        const val ANGLE_LEFT = 180f
        const val ANGLE_BOTTOM = 90f
    }
}

只需对描述布局执行相同操作:

在此处输入图像描述

如果您使用的是像 a 这样的视图,CardView它有一个内置的shapeAppearanceModel

cardView.shapeAppearanceModel = cardView.shapeAppearanceModel.toBuilder()
        .setTopRightCorner(concaveRoundedCornerTreatment).
        .........
        .build()
于 2020-07-15T05:25:24.843 回答