如何在ImgeSpan中从xml向量设置Drawable

时间:2017-05-22 13:21:50

标签: android drawable android-drawable android-vectordrawable

如何在Drawable内的xml向量中设置ImgeSpan? 我在下面使用这两种方法。但是我的图标没有显示出来。当我使用png从drawable资源图标出现。任何的想法?

private void setupEmptyListInfoBox() {
        Drawable icon = loadVectorFromResources(getActivity(), R.drawable.ic_add_circle_24dp);
        ImageSpan is = new ImageSpan(icon, DynamicDrawableSpan.ALIGN_BASELINE);
        String info = getString(R.string.empty_weight_list_info);
        int iconPosition = info.indexOf("|");

        SpannableString text = new SpannableString(info);
        text.setSpan(is, iconPosition, iconPosition + 1, 0);

        mEmptyListInfo.setText(text);
    }

public static Drawable loadVectorFromResources(Context context, int resId) {
    Drawable drawable;
    if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        drawable = VectorDrawableCompat.create(context.getResources(), resId, context.getTheme());
    } else {
        drawable = context.getResources().getDrawable(resId, context.getTheme());
    }
    return drawable;
}

1 个答案:

答案 0 :(得分:0)

我设法通过执行以下操作将矢量向量加载到ImageSpan

Drawable myDrawable = AppCompatResources.getDrawable(context, R.drawable.my_drawable);
myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());

ImageSpan myImageSpan = new ImageSpan(myDrawable, ImageSpan.ALIGN_BASELINE);
SpannableString mySpannableString = new SpannableString(context.getString(R.string.my_string));
mySpannableString.setSpan(myImageSpan, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

我希望有所帮助。