我正在尝试在 MultiAutoCompleteTextView
中创建联系人气泡,类似于它在 Google+ 应用中的实现方式.下面是截图:
I am trying to create contact bubbles in the MultiAutoCompleteTextView
similiar to how it is implemented in the Google+ app. Below is a screen shot:
.
我试图扩展 DynamicDrawableSpan
类,以便在一段文本的背景中获得一个可扩展的可绘制对象
I have tried to extend the DynamicDrawableSpan
class in order to get a spannable drawable in the background of a span of text
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;
}
}
我的椭圆形.xml 可绘制对象是这样定义的:
Where my oval.xml drawable is defined as so:
<?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>
在具有 MulitAutoCompleteTextView
的 Activity 类中,我将气泡跨度设置如下:
In my Activity class that has the MulitAutoCompleteTextView
, I set the bubble span like so:
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 个字符后面显示的不是椭圆形,而是字符不可见,并且背景中没有可绘制的椭圆形.
However, instead of the oval shape displaying behind the first 6 characters in the string, the characters are not visible and there is no oval drawable in the background.
如果我将 BubbleSpan 的 getDrawable() 方法更改为使用 .png 而不是可绘制的形状:
If i change the BubbleSpan's getDrawable() method to use a .png instead of a shape drawable:
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 的一部分的字符串中的字符将不会显示.如何使 span 中的字符显示在前景中,同时自定义形状 drawable 显示在背景中?
Then the .png will show up but the characters in the string that are a part of the span will not show up. How can I make it so that the characters in the span are displayed in the foreground, meanwhile a custom shape drawable gets displayed in the background?
我也尝试使用 ImageSpan
而不是子类化 DynamicDrawableSpan
但没有成功.
I attempted to also use an ImageSpan
instead of subclassing DynamicDrawableSpan
but was unsuccessful.
感谢@chrish 的所有帮助.所以我是这样做的:
Thanks @chrish for all the help. So here is how i did it:
final SpannableStringBuilder sb = new SpannableStringBuilder();
TextView tv = createContactTextView(contactName);
BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);
bd.setBounds(0, 0, bd.getIntrinsicWidth(),bd.getIntrinsicHeight());
sb.append(contactName + ",");
sb.setSpan(new ImageSpan(bd), sb.length()-(contactName.length()+1), sb.length()-1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
to_input.setText(sb);
public TextView createContactTextView(String text){
//creating textview dynamically
TextView tv = new TextView(this);
tv.setText(text);
tv.setTextSize(20);
tv.setBackgroundResource(R.drawable.oval);
tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_clear_search_api_holo_light, 0);
return tv;
}
public static Object convertViewToDrawable(View view) {
int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
view.measure(spec, spec);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.translate(-view.getScrollX(), -view.getScrollY());
view.draw(c);
view.setDrawingCacheEnabled(true);
Bitmap cacheBmp = view.getDrawingCache();
Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
view.destroyDrawingCache();
return new BitmapDrawable(viewBmp);
}
这篇关于联系气泡编辑文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!