我正在尝试在 MultiAutoCompleteTextView
中创建联系人气泡,类似于它在 Google+ 应用程序中的实现方式。下面是一个屏幕截图:
.
我试图扩展 DynamicDrawableSpan
类,以便在一段文本的背景中获得可跨越的可绘制对象
public class BubbleSpan extends DynamicDrawableSpan {
private Context c;
public BubbleSpan(Context context) {
super();
c = context;
}
@Override
public Drawable getDrawable() {
Resources res = c.getResources();
Drawable d = res.getDrawable(R.drawable.oval);
d.setBounds(0, 0, 100, 20);
return d;
}
}
我的 oval.xml 可绘制对象定义如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#352765"/>
<padding android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
<corners android:radius="6dp" />
</shape>
在我的 Activity 类中有 MulitAutoCompleteTextView
,我设置气泡跨度如下:
final Editable e = tv.getEditableText();
final SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append("some sample text");
sb.setSpan(new BubbleSpan(getApplicationContext()), 0, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
e.append(sb);
但是,在字符串的前 6 个字符后面显示的不是椭圆形,而是这些字符不可见,背景中也没有椭圆形可绘制对象。
如果我将 BubbleSpan 的 getDrawable() 方法更改为使用 .png 而不是可绘制的形状:
public Drawable getDrawable() {
Resources res = c.getResources();
Drawable d = res.getDrawable(android.R.drawable.bottom_bar);
d.setBounds(0, 0, 100, 20);
return d;
}
然后 .png 将显示,但字符串中作为跨度一部分的字符将不会显示。我怎样才能使 span 中的字符显示在前景中,同时自定义形状 drawable 显示在背景中?
我也尝试使用 ImageSpan
而不是子类 DynamicDrawableSpan
但没有成功。
原文由 Etienne Lawlor 发布,翻译遵循 CC BY-SA 4.0 许可协议
感谢@chrish 提供的所有帮助。所以我是这样做的: