1

我尝试为每个单词显示一个带有“芯片”样式的 TextView。换句话说,我希望这个Splitwise TokenAutoComplete不可编辑且不可自动完成。

所以我有一个适合我风格的drawable:

肌肉令牌背景.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/primary_dark" />
<corners
    android:topLeftRadius="5dp"
    android:bottomLeftRadius="5dp"
    android:topRightRadius="5dp"
    android:bottomRightRadius="5dp" />
</shape>

我有我的风格,它使用 drawable :

主题.xml

<resources>
    <style> 
        .....
    </style>
    <style name="textview_chips" parent="@android:style/TextAppearance.Medium">
        <item name="android:background">@drawable/muscle_token_background</item>
    </style>
</resources>

我找到的解决方案是使用SpannableStringBuilder和 SyleSpan :

SpannableStringBuilder sb = new SpannableStringBuilder(myString);
int currentCharCount = sb.length();
sb.append(myString.trim());
int nextCharCount = sb.length();
sb.setSpan(new StyleSpan(R.style.textview_chips), currentCharCount, nextCharCount, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(sb);

但是 TextView 只会变成斜体。

我尝试在 XML 文件中使用其他 TextView 并且背景可绘制对象显示良好

    <TextView
    ...
    style="@style/textview_chips"
    ... />

那么我该如何为我的 TextView 的每个单词应用这种样式呢?

4

1 回答 1

0

我终于做到了!

MyUtils.class

    public static Object convertViewToDrawable(View view) {
    int spec = View.MeasureSpec.makeMeasureSpec(0, View.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);
}

我的活动

String regex = "\\w+";
Pattern p = Pattern.compile(regex);
Matcher matcher = p.matcher(myString);
SpannableStringBuilder sb = new SpannableStringBuilder(myString);
while (matcher.find()) {
        final LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View parent =  (View) layoutInflater.inflate(R.layout.muscle_token, null);
        final TextView oneWord =  (TextView) parent.findViewById(R.id.muscle_name); // The TextView to convert into Drawable
        final int begin = matcher.start();
        final int end = matcher.end();
        float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 15, getResources().getDisplayMetrics());
        oneWord.setTextSize(pixels);
        oneWord.setText(concatString.substring(begin, end).toString());
        BitmapDrawable bd = (BitmapDrawable) MyUtils.convertViewToDrawable(oneWord);
        bd.setBounds(0, 0, bd.getIntrinsicWidth(),bd.getIntrinsicHeight());
        sb.setSpan(new ImageSpan(bd), begin, end, 0);
 }
 myTextView.setText(sb);

肌肉令牌.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">

<TextView android:id="@+id/muscle_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/muscle_token_background"
    android:padding="5dp"
    android:textColor="@color/icons"
    android:textSize="15sp" />

</LinearLayout>

感谢 我如何在 TextView 中的每个单词上使用 onTouchListeners? https://krishnalalstha.wordpress.com/2012/06/07/making-gosmsproevernote-like-edittext/

于 2015-07-26T03:21:14.970 回答