我有一个TextView,我想通过XML在我的文本中添加一个子弹符号。这可能吗?
当前回答
你可以尝试Android文档中描述的BulletSpan。
SpannableString string = new SpannableString("Text with\nBullet point");
string.setSpan(new BulletSpan(40, color, 20), 10, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
其他回答
在任何文本视图中添加bullet的另一个最佳方法是以下两个步骤:
首先,创建一个可绘制对象
<?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>或<li> html元素,我必须这样做
<string name="names"><![CDATA[<p><h2>List of Names:</h2></p><p>•name1<br />•name2<br /></p>]]></string>
如果你想保持自定义空间,那么使用</pre>标签
您必须使用正确的字符编码来实现此效果。你可以试试•
更新
Just to clarify: use `setText("\u2022 Bullet");` to add the bullet programmatically. `0x2022 = 8226`你可以尝试Android文档中描述的BulletSpan。
SpannableString string = new SpannableString("Text with\nBullet point");
string.setSpan(new BulletSpan(40, color, 20), 10, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
这招对我很管用:
<string name="text_with_bullet">Text with a \u2022</string>
推荐文章
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件
- 修改抽射超时时间
- 如何通过数据从第二个活动到第一个活动时按回?——安卓
- 如何在android中获得当前前景活动上下文?
- 如何在Android中获取当前日期?
- 获取Android设备名称
- 在WebView中上传文件
- 加载HTML文件到WebView
- 文件名中允许的字符
- Android:为什么视图没有maxHeight ?
- 如何获得具有已知资源名称的资源id ?
- 在Android上将字符串转换为整数
- 为什么“System.out。”println“工作在Android?