我只使用一个 TextView 并使用 spanable 向字符串添加字体和颜色。但是我在使用 spanable 将圆形背景显示为字符串时遇到问题。那么我如何使用 spannable String 概念实现这一点,图像如下所示。
最佳答案
对于 TextView
的背景颜色,您可以使用
String myString = "myString";
Spannable spanna = new SpannableString(myString);
spanna.setSpan(new BackgroundColorSpan(0xFFCCCCCC),0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(spanna);
并为圆形 TextView 创建自定义 XML 并为该 TextView 设置背景。
编辑
创建一个名为 rounded_corner.xml 的 XML 并设置此内容
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- view background color -->
<solid android:color="#ffffff" >
</solid>
<!-- view border color and width -->
<stroke
android:width="1dp"
android:color="#1c1b20" >
</stroke>
<!-- If you want to add some padding -->
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" >
</padding>
<!-- Here is the corner radius -->
<corners android:radius="10dp" >
</corners>
</shape>
然后将这一行添加到您的 XML TextView 中
android:background="@drawable/rounded_corner"
关于android - android中spanableString的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25724117/