【问题标题】:Adding text on the left and right sides of Canvas在 Canvas 的左右两侧添加文本
【发布时间】:2026-01-05 22:45:02
【问题描述】:

我正在创建一个自定义画布,如下图所示,左侧是文本,右侧还需要文本,如下图所示。我在左侧有部分文本,但是如何让文本在画布的左右显示?

这是我想要展示的内容:

当前代码:

private val textSize: Float = 70.toFloat()
private val groupSpacing = 100
private val paint = Paint()
init {
    paint.textSize = textSize
}
override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
    for (i in 0 until parent.childCount) {
        val view = parent.getChildAt(i)
        val position = parent.getChildAdapterPosition(view)
        if (position == 0) {
            c.drawText("  Default", view.left.toFloat(),
                    view.top - groupSpacing / 2 + textSize / 3, paint)
        } else if (position == 1) {
            c.drawText("  Other", view.left.toFloat(),
                    view.top - groupSpacing / 2 + textSize / 3, paint)
        }
    }
}

上面的代码只显示默认

【问题讨论】:

    标签: android canvas text right-align leftalign


    【解决方案1】:

    我能够通过以下方式实现这一目标:

    class CustomItemDecorator(context: Context) : RecyclerView.ItemDecoration() {
        private var textSize: Float
        private val groupSpacing = 100
        private val paint = Paint()
        private val dragPaint = Paint()
    
        init {
            val dpSize: Int = context.resources.getDimensionPixelSize(R.dimen.decorator_font_size)
            textSize = dpSize * context.resources.displayMetrics.scaledDensity
            paint.textSize = textSize
    
    
            paint.color = context.resources.getColor(R.color.venue_grey)
            paint.typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD)
    
            dragPaint.textSize = textSize
            dragPaint.color = context.resources.getColor(R.color.venue_grey)
        }
    
        override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
            super.onDrawOver(c, parent, state)
            for (i in 0 until parent.childCount) {
                val view = parent.getChildAt(i)
                val position = parent.getChildAdapterPosition(view)
                if (position == 0) {
                    paint.textAlign = Paint.Align.LEFT
                    c.drawText("DEFAULT", 35f,
                            view.top - groupSpacing / 2 + textSize / 3, paint)
                    dragPaint.textAlign = Paint.Align.RIGHT
                    c.drawText("Some Text that is off to the right", view.right.toFloat() - 30,
                            view.top - groupSpacing / 2 + textSize / 3, dragPaint)
                } else if (position == 1) {
                    val x = view.x + 30
                    view.width
                    //the alignment below may need to be adjusted to 80 or 90?
                    paint.textAlign = Paint.Align.LEFT
                    c.drawText("OTHER", 35f,
                            view.top - groupSpacing / 2 + textSize / 3, paint)
                }
            }
        }
    
        override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
            if (parent.getChildAdapterPosition(view) == 0 || parent.getChildAdapterPosition(view) == 1) {
                outRect.set(0, groupSpacing, 0, 0)
            }
        }
    }
    

    【讨论】: