Textview Android Kotlin中的订单列表

Order List in Textview Android Kotlin(Textview Android Kotlin中的订单列表)

本文介绍了Textview Android Kotlin中的订单列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想在Android Kotlin的文本视图中按顺序列表显示文本。我试过Ordered List inside an Android Textview。我尝试了以下代码,它可以正常工作,但它没有匹配数字文本的高度。我将分享我到目前为止所做的代码。

NumberIndentspan.kt

import android.graphics.Canvas
import android.graphics.Paint
import android.text.Layout
import android.text.style.LeadingMarginSpan

class NumberIndentSpan(
    private val leadWidth: Int,
    private val gapWidth: Int,
    private val index: Int
) : LeadingMarginSpan {

    override fun getLeadingMargin(first: Boolean): Int = leadWidth + gapWidth

    override fun drawLeadingMargin(
        c: Canvas?,
        p: Paint?,
        x: Int,
        dir: Int,
        top: Int,
        baseline: Int,
        bottom: Int,
        text: CharSequence?,
        start: Int,
        end: Int,
        first: Boolean,
        layout: Layout?
    ) {
        if (first) {
            val orgStyle = p!!.style
            p.style = Paint.Style.FILL
            val width = p.measureText("4.")
            c!!.drawText("$index.", (leadWidth + x - width / 2) * dir, bottom - p.descent(), p)
            p.style = orgStyle
        }
    }
}

MainActivity.kt

 val list = mutableListOf(
                "Log in Google account
",
                "Scroll to the page
",
                "Tap disconnect from account to logout"
            )
            var number = 1
            val builder = SpannableStringBuilder()
            for (text in list) {
                val contentStart: Int = builder.length
                builder.append(text)
                builder.setSpan(NumberIndentSpan(15, 15, number), contentStart, builder.length, 0)
                number++
            }
            textView.text = builder

我要将数字文本放在相同的高度。因为图像显示了我从这个代码中得到了什么。我也遇到了格式问题,如第一个点和第二个点文本开头不同,我想按顺序进行。我怎么才能修好所有这些东西。提前感谢

推荐答案

我遇到了同样的问题。这是因为您应用了自定义字体,并且很可能在Y定位期间没有考虑到额外的行距。

这是我的工作解决方案:https://gist.github.com/marcelpallares/ae6285ee0dcb3f04493991dcb18d01bd

class NumberIndentSpan(
    private val number: Int,
    private val leadWidth: Int = 15,
    private val gapWidth: Int = 30,
) : LeadingMarginSpan {
    override fun getLeadingMargin(first: Boolean): Int {
        return leadWidth + gapWidth
    }

    override fun drawLeadingMargin(
        canvas: Canvas,
        paint: Paint,
        x: Int,
        dir: Int,
        top: Int,
        baseline: Int,
        bottom: Int,
        t: CharSequence?,
        start: Int,
        end: Int,
        first: Boolean,
        layout: Layout?
    ) {
        if (first) {
            val text = "$number."
            val width = paint.measureText(text)

            val yPosition = baseline.toFloat()
            val xPosition = (leadWidth + x - width / 2) * dir

            canvas.drawText(text, xPosition, yPosition, paint)
        }
    }
}

这篇关于Textview Android Kotlin中的订单列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Textview Android Kotlin中的订单列表

基础教程推荐