【发布时间】:2011-03-26 15:58:12
【问题描述】:
我有一个 TextView,我想通过 XML 在我的文本中添加一个项目符号。有可能吗?
【问题讨论】:
标签: android android-layout special-characters textview
我有一个 TextView,我想通过 XML 在我的文本中添加一个项目符号。有可能吗?
【问题讨论】:
标签: android android-layout special-characters textview
您必须使用正确的character encoding 才能完成此效果。你可以试试•
澄清一下:使用setText("\u2022 Bullet"); 以编程方式添加项目符号。 0x2022 = 8226
【讨论】:
setText("\u2022 Bullet"); 以编程方式添加项目符号。 0x2022 = 8226
• = \u2022, ● = \u25CF, ○ = \u25CB, ▪ = \u25AA, ■ = \u25A0, □ = \u25A1, ► = \u25BA
这对我有用:
<string name="text_with_bullet">Text with a \u2022</string>
【讨论】:
复制粘贴: •。我已经用其他奇怪的字符完成了它,例如 ◄ 和 ►。
编辑: here 就是一个例子。底部的两个Buttons 有android:text="◄" 和"►"。
【讨论】:
Prolly 有一个更好的解决方案,但这就是我所做的。
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TableRow>
<TextView
android:layout_column="1"
android:text="•"></TextView>
<TextView
android:layout_column="2"
android:layout_width="wrap_content"
android:text="First line"></TextView>
</TableRow>
<TableRow>
<TextView
android:layout_column="1"
android:text="•"></TextView>
<TextView
android:layout_column="2"
android:layout_width="wrap_content"
android:text="Second line"></TextView>
</TableRow>
</TableLayout>
它的工作方式如你所愿,但确实是一种解决方法。
【讨论】:
您可以按照 Android 文档中的说明尝试 BulletSpan。
SpannableString string = new SpannableString("Text with\nBullet point");
string.setSpan(new BulletSpan(40, color, 20), 10, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
【讨论】:
这就是我最终这样做的方式。
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/circle"
android:drawableStart="@drawable/ic_bullet_point" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Your text"
android:textColor="#000000"
android:textSize="14sp" />
</LinearLayout>
drawbale/circle.xml 的代码是
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thickness="5dp"
android:useLevel="false">
<solid android:color="@color/black1" />
</shape>
【讨论】:
使用 Unicode 我们可以很容易地做到这一点,但是如果想改变子弹的颜色,我尝试使用彩色子弹图像并将其设置为可绘制的左侧并且它起作用了
<TextView
android:text="Hello bullet"
android:drawableLeft="@drawable/bulleticon" >
</TextView>
【讨论】:
在任何文本视图中添加项目符号的另一种最佳方法如下两步所述:
首先,创建一个可绘制对象
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!--set color of the bullet-->
<solid
android:color="#666666"/> //set color of bullet
<!--set size of the bullet-->
<size
android:width="120dp"
android:height="120dp"/>
</shape>
然后在textview中添加这个drawable并使用下面的属性设置它的pedding
android:drawableStart="@drawable/bullet"
android:drawablePadding="10dp"
【讨论】:
由于 android 不支持<ol>, <ul> or <li> html 元素,我不得不这样做
<string name="names"><![CDATA[<p><h2>List of Names:</h2></p><p>•name1<br />•name2<br /></p>]]></string>
如果您想维护自定义空间,请使用</pre> tag
【讨论】:
(几乎)所有选项都与使用html 标签有关。
你可以为你的 TextView 使用 drawables如果它只有一行文本。
类似这样的:
<TextView
android:id="@+id/tv_with_bullet"
android:layout_width="match_parent"
android:layout_height="50dp"
app:drawableStartCompat="@drawable/ic_desired_bullet_icon" />
并在 SVG 中添加所需的可绘制项目符号。它实际上不占用空间,让您无需添加复杂的string literals。您也可以在 here 中下载 SVG 文件以获取要点
【讨论】: