bro*_*wep 180
如果不支持ul/li/ol,那就很难做到.幸运的是,你可以使用它作为语法糖:
• foo<br/>
• bar<br/>
• baz<br/>
Run Code Online (Sandbox Code Playgroud)
•是列表子弹的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)
Die*_*ner 53
browep解释了HTML的方式.使用html实体提供的解决方案非常有用.但它只包括子弹.如果文本换行,则缩进将不正确.
我找到了其他嵌入Web视图的解决方案.这对某些人来说可能是合适的,但我认为它有点过分......(与使用列表视图相同.)
我使用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)正:
负:
Ish*_*iaq 34
我找到了一个替代品..只需复制这个子弹"•"(它是一个文本)并粘贴到文本视图的文本中,您可以通过更改文本颜色以及所有其他属性(如大小,高度宽度)来更改项目符号颜色. .. :)
您可以在打字时使用快捷方式获取此子弹
对于窗户
ALT + 7
对于mac
ALT + 8
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)
到目前为止,这是最简单的。
<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)
刚刚注意到我 8 年前的答案,并决定根据最新的 Android 文档更新它,以使用 HTML 标记设置文本样式:
\n<ul>&等 HTML 标签。<li>支持这些标签和许多其他标签!setText()\xe2\x80\xa2如果您想在多行字符串中保留缩进,这比直接粘贴字符或其十六进制代码更好。可以使用字符串资源中的<ul>和标签来创建项目符号列表。<li>
不要使用 setText(Html.fromHtml(string))在代码中设置字符串!通常只需在 xml 中或使用 setText( string ) 设置字符串即可。
\n例如:
\nstrings.xml 文件
\n<string name="str1">\n <ul>\n <li><i>first</i> item</li>\n <li>item 2</li>\n </ul>\n</string>\nRun Code Online (Sandbox Code Playgroud)\n | 归档时间: |
|
| 查看次数: |
77035 次 |
| 最近记录: |