android:MultiAutoCompleteTextView样式就像gmail一样

时间:2016-01-26 13:13:09

标签: android android-view android-custom-view

我想像gmail app一样创建自定义MultiAutoCompleteTextView: enter image description here

我创建自定义视图并扩展到MultiAutoCompleteTextView并使用Span,但我有问题,如果没有足够的空间用于文本和图像然后跨度有(空格)然后查看拆分它们

enter image description here

我的代码是自定义视图:

public class CustomMultiAutoCompleteTextView extends MultiAutoCompleteTextView {

@Override
public void setTokenizer(Tokenizer t) {
    super.setTokenizer(t);
}

public CustomMultiAutoCompleteTextView(Context context) {
    super(context);
}

public CustomMultiAutoCompleteTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomMultiAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}


@Override
protected void replaceText(CharSequence text) {
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.avatar);
    SpannableStringBuilder builder = new SpannableStringBuilder();
    builder.append("g");
    builder.setSpan(new ImageSpan(getContext(), getRoundedBitmap(bitmap)),
            builder.length() - 1, builder.length(), 0);
    builder.append(text);
    builder.setSpan(new BackgroundColorSpan(Color.GRAY), 1, builder.length(), 0);
    super.replaceText(builder);

}

public static Bitmap getRoundedBitmap(Bitmap bitmap) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int color = Color.RED;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    bitmap.recycle();

    return output;
}


}

所以任何机构都有建议吗?

1 个答案:

答案 0 :(得分:1)

检查这两个库:

他们正在做你想要完成的事情。

至少第一个库背后的想法 - 一旦HTTP/1.1 422 Unprocessable Entity Content-Length: 149 { "message": "Validation Failed", "errors": [ { "resource": "Issue", "field": "title", "code": "missing_field" } ] } 发生,你确实用onTextChanged创建Bitmap文本并设置TextView(淹没在{{} 1}})。

CompoundDrawablesWithIntrinsicBounds

图片+名称只是一个位图 - 它不能再被包裹,您的问题就解决了。