我有一个TextView,我想通过XML在我的文本中添加一个子弹符号。这可能吗?


当前回答

复制粘贴:我还用过其他一些奇怪的文字,比如“京城”和“►”。

编辑:这里有一个例子。最下面的两个按钮分别是“text=”和“►”。

其他回答

你可以尝试Android文档中描述的BulletSpan。

SpannableString string = new SpannableString("Text with\nBullet point");
string.setSpan(new BulletSpan(40, color, 20), 10, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

使用Unicode我们可以很容易地做到这一点,但如果想改变子弹的颜色,我尝试了彩色子弹图像,并将其设置为drawableStart,它工作

<TextView     
    android:text="Hello bullet"
    android:drawableStart="@drawable/bulleticon" >
</TextView>

这招对我很管用:

<string name="text_with_bullet">Text with a \u2022</string>

(几乎)所有的选项都是关于使用HTML标记的。

你可以使用drawables为你的TextView,如果它只有一行文本。

就像这样:

<TextView
            android:id="@+id/tv_with_bullet"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            app:drawableStartCompat="@drawable/ic_desired_bullet_icon" />

并在SVG中添加所需的子弹图。它不占用任何空间,使您无需添加复杂的字符串字面量。您还可以在这里下载项目符号的SVG文件

肯定有更好的解决办法,但我就是这么做的。

<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>

就像你想要的那样,但实际上是一种变通方法。