2

我正在尝试MultiAutoCompleteTextView从用户那里获取文本并以类似气泡的格式显示它们,但我得到的是:宽度和高度必须在 android 中 > 0

    final MultiAutoCompleteTextView tags = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
    String[] TAGS = getResources().getStringArray(R.array.countries);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, TAGS);
    tags.setAdapter(adapter);
    tags.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    tags.setThreshold(1);
    String contactName = tags.getText().toString();
    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);
    Toast.makeText(getApplicationContext(),sb,Toast.LENGTH_SHORT).show();

}

    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 = 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);

    }

当我尝试运行它时,我得到这个错误宽度和高度必须大于 0。

  E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.androidbegin.train, PID: 11263
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidbegin.train/com.androidbegin.loginregister.WorkNow}: java.lang.IllegalArgumentException: width and height must be > 0
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:155)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:135)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                                                Caused by: java.lang.IllegalArgumentException: width and height must be > 0
                                                   at android.graphics.Bitmap.createBitmap(Bitmap.java:836)
                                                   at android.graphics.Bitmap.createBitmap(Bitmap.java:815)
                                                   at android.graphics.Bitmap.createBitmap(Bitmap.java:782)
                                                   at com.androidbegin.loginregister.WorkNow.convertViewToDrawable(WorkNow.java:57)
                                                   at com.androidbegin.loginregister.WorkNow.onCreate(WorkNow.java:34)
                                                   at android.app.Activity.performCreate(Activity.java:6010)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:155) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
4

1 回答 1

6

我认为主要问题是spec被设置为0

int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

然后,在这里,视图将具有0宽度和高度......

view.measure(spec, spec);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

因此,在创建位图时,Exception会发生 an ,因为view.getMeasuredWidth()and view.getMeasuredHeight()are both0并且它们不能是

Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

如果可能,请尝试更改为:

int spec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.UNSPECIFIED);
于 2016-07-24T04:03:54.250 回答