android textview设置html中图片显示问题

显示本地mipmap/drawable文件下图片:

string info = "<img src=\"img_talk_peaple_only\"/>";

text.setText(Html.fromHtml(info, new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(String source) {
                Drawable drawable = null;
                try {
                    Field resource = R.mipmap.class.getField(source);
                    int rId = Integer.parseInt(resource.get(null).toString());
                    drawable = context.getResources().getDrawable(rId);
                    drawable.setBounds(0, 0, DensityUtils.sp2px(QADetailActivity.this,16),
                            DensityUtils.sp2px(QADetailActivity.this,16));
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return drawable;
            }
        }, null));

//注意:Field resource = R.mipmap.class.getField(source);如果是drawable下的图片,换成drawable;图片所在的moudle要和方法调用的类在一个moudle,不然,反射的r文件不一致,获取不到图片。
tips: 如果不设置android:lineSpacingExtra属性时,能很好的显示,但是如果设置了该属性后,图片会下移,占用行间距
关于图片和文字无法对齐的问题,尚未找出合适解决方案

猜你喜欢

转载自blog.csdn.net/github_37610197/article/details/82257887