TextView富文本显示

1.使用类似Html的标签显示图像

        tv = (TextView) findViewById(R.id.text);
        ImageGetter imageGetter = new ImageGetter() {

            @Override
            public Drawable getDrawable(String source) {
                // TODO Auto-generated method stub
                Drawable drawable = getResources().getDrawable(
                        R.drawable.ic_launcher);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                        drawable.getIntrinsicHeight());
                return drawable;
            }
        };
        Spanned spanned = Html.fromHtml(
                "这是一个富文本,然后加一个图标",
                imageGetter, null);
        tv.setText(spanned);
        tv.getPaint().setAntiAlias(true);// 抗锯齿

2.使用ImageSpan在TextView上显示图像

      TextView textView = (TextView) findViewById(R.id.textsss);
        //根据资源id获取资源的bitmap对象
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        //根据bitmap创建ImageSpan对象
        ImageSpan imageSpan = new ImageSpan(getApplicationContext(), bitmap);
        //方便插入ImageSpan对象
        SpannableString spannableString = new SpannableString("icon");
        //ImageSpan对象替换icon的从0开始的2位
        spannableString.setSpan(imageSpan, 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(spannableString);

3.TextView点击链接操作

    SpannableString spannableString = new SpannableString("icon");
        //ImageSpan对象替换icon的从0开始的2位
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {

            }
        },0,2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(spannableString);
        //在单击链接凡是有操作的,都需要设置MovementMethod
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }

你可能感兴趣的:(Android技术积累)