排序 android
Bulleted lists or numbered lists are a pretty common way of summing up content, but how do we actually implement those in Android? Let’s take a quick look at how we can achieve these lists in a simple TextView!
项目符号列表或编号列表是汇总内容的一种很常见的方法,但是我们如何真正在Android中实现这些项目呢? 让我们快速看一下如何在简单的TextView中获得这些列表!
So let’s start off with ‘the easy one’: a bulleted list. It’s safe to assume you know what I’m talking about, but here’s an example just for the record:
因此,让我们从“简单的一个”开始:一个项目符号列表。 可以肯定地说,您知道我在说什么,但这是一个示例,仅供参考:
Item 1 项目1 Item 2 项目2 Item 3 项目3Let’s say we have this content available as a simple list:
假设我们将这些内容作为简单列表提供:
val items = ["Item 1", "Item 2", "Item 3"]We’ll want to add a bullet point to each item, but also make sure that the bullet point snaps to the top of the TextView whenever the content has multiple lines. To achieve this, we’ll take a look at using spans!
我们将要为每个项目添加一个项目符号,但还要确保只要内容有多行,项目符号都将捕捉到TextView的顶部。 为了实现这一点,我们将看看使用跨度!
Start off with a simple SpannableStringBuilder, we’ll take this as our starting point and append our lines one by one:
从一个简单的SpannableStringBuilder ,我们将以此为起点,并SpannableStringBuilder追加行:
val builder = SpannableStringBuilder()So once we have our builder we’ll simply loop through our list, add some linebreaks and use a BulletSpan to implement the actual bullet points:
因此,一旦有了构建器,我们就可以简单地遍历列表,添加一些换行符并使用BulletSpan来实现实际的要点:
val items = ["Item 1", "Item 2", "Item 3"] val builder = SpannableStringBuilder() items.forEach { item -> builder.append( item + "\n\n", // Add some linebreaks BulletSpan(), // Add the bullet point span Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ) }BulletSpan implements LeadingMarginSpan, which basically offsets your text by a certain amount. In the case of BulletSpan, it’ll add a bullet point within the offset area.
BulletSpan工具LeadingMarginSpan ,基本上抵消了一定量的文本。 对于BulletSpan ,它将在偏移区域内添加一个项目符号点。
In case you’d like to set the offset area manually or set the color of the bullet point, simply use the optionalgapWidth and color parameters of BulletSpan!
如果你想手动设置偏移区或设置子弹点的颜色,只需使用可选的gapWidth和color的参数BulletSpan !
Now all you need to do is set the builder as the text of your TextView and done! A bulleted list in a TextView 😄
现在,您需要做的就是将builder设置为TextView的文本并完成! TextView项目符号列表
So first of all: what do we mean with a numbered list? It’s basically a bulleted list, but instead of bullets we’ll number each bullet point. If you're familiar with HTML, compare it with an ordered list:
所以首先:编号列表是什么意思? 这基本上是一个项目符号列表,但是我们将为每个项目符号编号,而不是项目符号。 如果您熟悉HTML,请将其与有序列表进行比较:
Item 1 项目1 Item 2 项目2 Item 3 项目3The basics of implementing a numbered list are the same as how we did the bulleted list, but we’ll need a different span for that. Android doesn’t have any spans to support this out of the box, but luckily it’s pretty easy to create our own! We can simply implement LeadingMarginSpan ourselves and add the behavior we need in there. By implementing, we’ll get 2 methods to override: drawLeadingMargin and getLeadingMargin:
实现编号列表的基本原理与项目符号列表的实现方法相同,但是我们需要不同的跨度。 Android没有开箱即用的功能来支持此功能,但是幸运的是,创建我们自己的功能非常容易! 我们可以简单地自己实现LeadingMarginSpan并在其中添加所需的行为。 通过实现,我们将获得2种方法来覆盖: drawLeadingMargin和getLeadingMargin :
drawLeadingMargin takes care of rendering the leading margin
drawLeadingMargin负责渲染前导边距
getLeadingMargin simply returns the width of the leading margin.
getLeadingMargin只是返回前导边距的宽度。
We’ll pass 2 things to our span: the width of the leading margin and the text (in this case: number) we’d like to draw within the leading margin:
我们将两件事传递给我们的跨度:领先行距的宽度和我们想要在领先行距内绘制的文本(在这种情况下:数字):
class OrderedListSpan( private val width: Int, private val leadingText: String ) : LeadingMarginSpan { override fun drawLeadingMargin( canvas: Canvas, paint: Paint, x: Int, dir: Int, top: Int, baseline: Int, bottom: Int, text: CharSequence, start: Int, end: Int, first: Boolean, layout: Layout ) { // Check if we're at the start of the span val spanStart = (text as Spanned).getSpanStart(this) val isFirstCharacter = spanStart == start // If so, draw the text in the leading span if (isFirstCharacter) { canvas.drawText(leadingText, x.toFloat(), baseline.toFloat(), paint) } } override fun getLeadingMargin(first: Boolean): Int = width }That’s all there is! Now we can simply use this span the same as we did with the bulleted list, the only thing we need to pass here is the width of the leading margin and the number we want to show:
这就是全部! 现在,我们可以像使用项目符号列表一样简单地使用此跨度,我们唯一需要在此处传递的是前导边距的宽度和我们想要显示的数字:
const val WIDTH = 70 val items = ["Item 1", "Item 2", "Item 3"] val builder = SpannableStringBuilder() items.forEachIndexed { index, item -> builder.append( item + "\n\n", OrderedListSpan(WIDTH, "$index."), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ) }As a finishing touch, hide this behind a nice extension method and you’ll implement ordered & unordered lists in no time!
最后,将其隐藏在不错的扩展方法后面,您将立即实现有序列表和无序列表!
And that’s it, guys! Ordered or unordered lists in TextViews on Android 😄 Any questions? Here at PINCH, we’re glad to help people out, just hit me up. Happy coding ya’ll!
就是这样,伙计们! Android上的TextView中的有序列表或无序列表 😄有问题吗? 在 PINCH ,我们很高兴为人们提供帮助,打了我一下。 祝您编码愉快!
翻译自: https://itnext.io/un-ordered-lists-on-android-20defac6b156
排序 android