欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TextView setText 和 插入图片

程序员文章站 2024-03-24 11:12:16
...

setTextSize(int unit, int size)
第一个参数可设置如下静态变量:
TypedValue.COMPLEX_UNIT_PX : Pixels
TypedValue.COMPLEX_UNIT_SP : Scaled Pixels
TypedValue.COMPLEX_UNIT_DIP : Device Independent Pixels

  tvMeDec.setText(Html.fromHtml(descString(), getImageGetterInstance(),
                null));

  private String descString() {
        return "生活不止眼前的苟且,还有诗和远方的苟且 " + "<img src='" + R.mipmap.icon_edit + "'/>";
    }

 public Html.ImageGetter getImageGetterInstance() {
        Html.ImageGetter imgGetter = new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(String source) {
                int id = Integer.parseInt(source);
                Drawable d = getResources().getDrawable(id);
                int wdp = dip2px(UIUtils.getContext(), 20);
                int hdp = dip2px(UIUtils.getContext(), 20);
                d.setBounds(0, 0, wdp, hdp);
                return d;
            }
        };
        return imgGetter;
    }



public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
相关标签: TextView