我有一个TextView,我想通过XML在我的文本中添加一个子弹符号。这可能吗?
当前回答
肯定有更好的解决办法,但我就是这么做的。
<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>
就像你想要的那样,但实际上是一种变通方法。
其他回答
您必须使用正确的字符编码来实现此效果。你可以试试•
更新
Just to clarify: use `setText("\u2022 Bullet");` to add the bullet programmatically. `0x2022 = 8226`由于android不支持<ol>, <ul>或<li> html元素,我必须这样做
<string name="names"><![CDATA[<p><h2>List of Names:</h2></p><p>•name1<br />•name2<br /></p>]]></string>
如果你想保持自定义空间,那么使用</pre>标签
这就是我最后怎么做的。
<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>
(几乎)所有的选项都是关于使用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文件
这招对我很管用:
<string name="text_with_bullet">Text with a \u2022</string>
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件