問題描述
我正在嘗試在 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模板網!