如何将项目符号列表添加到Android应用程序?

use*_*849 87 android textview

我搜索了我的问题,但没有提供有效的答案.如何在我的textview中添加项目符号列表.

bro*_*wep 180

如果不支持ul/li/ol,那就很难做到.幸运的是,你可以使用它作为语法糖:

&#8226; foo<br/>
&#8226; bar<br/>
&#8226; baz<br/>
Run Code Online (Sandbox Code Playgroud)

&#8226;是列表子弹的html实体更多的选择在这里http://www.elizabethcastro.com/html/extras/entities.html

更多关于Mark Murphy提供支持的标签(@CommonsWare) http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html 用Html.fromHtml加载它

((TextView)findViewById(R.id.my_text_view)).setText(Html.fromHtml(myHtmlString));
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果从values/strings.xml获取字符串(使用context.getString(R.string.yourstring);),则必须将其包装在_CDATA_中:`<string name ="string_name"> < ![CDATA [&#8226; foo <br />&#8226; bar ...]]> </ string>` (4认同)
  • 如果项目符号项中有多行,则不起作用 (4认同)

Die*_*ner 53

  1. browep解释了HTML的方式.使用html实体提供的解决方案非常有用.但它只包括子弹.如果文本换行,则缩进将不正确.

  2. 我找到了其他嵌入Web视图的解决方案.这对某些人来说可能是合适的,但我认为它有点过分......(与使用列表视图相同.)

  3. 我喜欢Nelson创造性方法:D,但它没有给你添加无序列表到文本视图的可能性.

  4. 使用BulletSpan的子弹无序列表示例

    CharSequence t1 = getText(R.string.xxx1);
    SpannableString s1 = new SpannableString(t1);
    s1.setSpan(new BulletSpan(15), 0, t1.length(), 0);
    CharSequence t2 = getText(R.string.xxx2);
    SpannableString s2 = new SpannableString(t2);
    s2.setSpan(new BulletSpan(15), 0, t2.length(), 0);
    textView.setText(TextUtils.concat(s1, s2));
    
    Run Code Online (Sandbox Code Playgroud)

正:

  • 文本换行后具有正确缩进的项目符号.
  • 您可以在TextView的一个实例中组合其他格式化或未格式化的文本
  • 您可以在BulletSpan构造函数中定义缩进应该有多大.

负:

  • 您必须将列表中的每个项目保存在单独的字符串资源中.因此,您无法定义自己的HTML列表.

  • 上面的代码对我有用!!! 我所要做的就是在xml中每个字符串的末尾添加"\n".... (5认同)

Ish*_*iaq 34

我找到了一个替代品..只需复制这个子弹"•"(它是一个文本)并粘贴到文本视图的文本中,您可以通过更改文本颜色以及所有其他属性(如大小,高度宽度)来更改项目符号颜色. .. :)

您可以在打字时使用快捷方式获取此子弹

对于窗户

ALT + 7

对于mac

ALT + 8

  • Alt+7 对我不起作用(也许它只是 mac 或 linux 的东西)但是复制和粘贴 unicode 字符 • 起作用了。 (2认同)
  • 仅供参考:ALT + 7 只有在键盘有单独的小键盘时才有效。 (2认同)
  • \u2022 是答案 (2认同)

cot*_*aws 21

受到各种答案的启发,我创建了一个Utility类,使其成为一个简单的单行.这将创建一个带有包装文本缩进的项目符号列表.它具有组合字符串,字符串资源和字符串数组资源的方法.

它将创建一个CharSequence,您可以将其传递给TextView.例如:

CharSequence bulletedList = BulletListUtil.makeBulletList("First line", "Second line", "Really long third line that will wrap and indent properly.");
textView.setText(bulletedList);
Run Code Online (Sandbox Code Playgroud)

希望它有用.请享用.

注意:这将使用系统标准项目符号,一个与文本颜色相同的小圆圈.如果你想要一个自定义项目符号,可以考虑继承BulletSpan并覆盖它drawLeadingMargin()以绘制你想要的项目符号.看一下BulletSpan源代码,了解它是如何工作的.

public class BulletTextUtil {

/**
 * Returns a CharSequence containing a bulleted and properly indented list.
 *
 * @param leadingMargin In pixels, the space between the left edge of the bullet and the left edge of the text.
 * @param context
 * @param stringArrayResId A resource id pointing to a string array. Each string will be a separate line/bullet-point.
 * @return
 */
public static CharSequence makeBulletListFromStringArrayResource(int leadingMargin, Context context, int stringArrayResId) {
    return makeBulletList(leadingMargin, context.getResources().getStringArray(stringArrayResId));
}

/**
 * Returns a CharSequence containing a bulleted and properly indented list.
 *
 * @param leadingMargin In pixels, the space between the left edge of the bullet and the left edge of the text.
 * @param context
 * @param linesResIds An array of string resource ids. Each string will be a separate line/bullet-point.
 * @return
 */
public static CharSequence makeBulletListFromStringResources(int leadingMargin, Context context, int... linesResIds) {
    int len = linesResIds.length;
    CharSequence[] cslines = new CharSequence[len];
    for (int i = 0; i < len; i++) {
        cslines[i] = context.getString(linesResIds[i]);
    }
    return makeBulletList(leadingMargin, cslines);
}

/**
 * Returns a CharSequence containing a bulleted and properly indented list.
 *
 * @param leadingMargin In pixels, the space between the left edge of the bullet and the left edge of the text.
 * @param lines An array of CharSequences. Each CharSequences will be a separate line/bullet-point.
 * @return
 */
public static CharSequence makeBulletList(int leadingMargin, CharSequence... lines) {
    SpannableStringBuilder sb = new SpannableStringBuilder();
    for (int i = 0; i < lines.length; i++) {
        CharSequence line = lines[i] + (i < lines.length-1 ? "\n" : "");
        Spannable spannable = new SpannableString(line);
        spannable.setSpan(new BulletSpan(leadingMargin), 0, spannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        sb.append(spannable);
    }
    return sb;
}

}
Run Code Online (Sandbox Code Playgroud)


Mik*_*rin 13

即用型 Kotlin 扩展

fun List<String>.toBulletedList(): CharSequence {
    return SpannableString(this.joinToString("\n")).apply {
        this@toBulletedList.foldIndexed(0) { index, acc, span ->
            val end = acc + span.length + if (index != this@toBulletedList.size - 1) 1 else 0
            this.setSpan(BulletSpan(16), acc, end, 0)
            end
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

val bulletedList = listOf("One", "Two", "Three").toBulletedList()
label.text = bulletedList
Run Code Online (Sandbox Code Playgroud)

颜色和尺寸:

要更改项目符号颜色或大小,请使用 CustomBulletSpan 而不是 BulletSpan

package com.fbs.archBase.ui.spans

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.text.Layout
import android.text.Spanned
import android.text.style.LeadingMarginSpan
import androidx.annotation.ColorInt

class CustomBulletSpan(
        private val bulletRadius: Int = STANDARD_BULLET_RADIUS,
        private val gapWidth: Int = STANDARD_GAP_WIDTH,
        @ColorInt private val circleColor: Int = STANDARD_COLOR
) : LeadingMarginSpan {

    private companion object {
        val STANDARD_BULLET_RADIUS = Screen.dp(2)
        val STANDARD_GAP_WIDTH = Screen.dp(8)
        const val STANDARD_COLOR = Color.BLACK
    }

    private val circlePaint = Paint().apply {
    color = circleColor
        style = Paint.Style.FILL
        isAntiAlias = true
    }

    override fun getLeadingMargin(first: Boolean): Int {
        return 2 * bulletRadius + gapWidth
    }

    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?
    ) {
        if ((text as Spanned).getSpanStart(this) == start) {
            val yPosition = (top + bottom) / 2f
            val xPosition = (x + dir * bulletRadius).toFloat()

            canvas.drawCircle(xPosition, yPosition, bulletRadius.toFloat(), circlePaint)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Ami*_*kur 6

到目前为止,这是最简单的。

<string name="bullet_ed_list">\n\u2022 He has been Chairman of CFL Manufacturers Committee of ELCOMA, the All India Association of Lighting Equipment Manufacturers.
\n\u2022 He has been the President of Federation of Industries of India (FII).</string>
Run Code Online (Sandbox Code Playgroud)

  • 这并没有正确地换行到缩进,对吗?(例如下一行将换行在项目符号下方) (3认同)

Abh*_*hek 5

[2021 年 10 月更新]

\n

刚刚注意到我 8 年前的答案,并决定根据最新的 Android 文档更新它,以使用 HTML 标记设置文本样式

\n
    \n
  • 在字符串资源中使用<ul>&等 HTML 标签。<li>支持这些标签和许多其他标签!
  • \n
  • 如果您不应用格式设置,则可以通过调用直接设置 TextView 文本setText()
  • \n
  • \xe2\x80\xa2如果您想在多行字符串中保留缩进,这比直接粘贴字符或其十六进制代码更好。
  • \n
\n
\n
我2013年的旧答案:
\n

可以使用字符串资源中的<ul>和标签来创建项目符号列表。<li>

\n

不要使用 setText(Html.fromHtml(string))在代码中设置字符串!通常只需在 xml 中或使用 setText( string ) 设置字符串即可。

\n

例如:

\n

strings.xml 文件

\n
<string name="str1">\n    <ul>\n        <li><i>first</i> item</li>\n        <li>item 2</li>\n    </ul>\n</string>\n
Run Code Online (Sandbox Code Playgroud)\n
\n_layout.xml 文件_\n```\n\n```\n
\n它将产生以下结果:\n
  • 第一
  • 第2项
\n
\n支持以下标签(直接嵌入字符串资源中):\n
    \n
  • <a>(支持属性“href”)
  • \n
  • <注释>
  • \n
  • <b>
  • \n
  • <大>
  • \n
  • <font>(支持整数属性“height”、“size”、“fgcolor”和“bicolor”)
  • \n
  • <我>
  • \n
  • <li>
  • \n
  • <字幕>
  • \n
  • <小>
  • \n
  • <罢工>
  • \n
  • <子>
  • \n
  • <sup>
  • \n
  • <TT>
  • \n
  • <u>
  • \n
\n

  • 不起作用。支持的 html 标签仅为 &lt;b&gt;、&lt;i&gt; 和 &lt;u&gt;。http://developer.android.com/guide/topics/resources/string-resource.html (7认同)