突出显示的android文本视图 [英] Highlight text view in android

查看:269
本文介绍了突出显示的android文本视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想强调的TextView android系统中相同的这一形象

I want to highlight textview in android same this image

我该怎么办呢?谢谢

像作为这个链接 http://youtube.com/watch?v=0qE3egNettY

推荐答案

这是你想要的:

package org.pskink.shape;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.Picture;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PictureDrawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.widget.TextView;

public class DrawableSpanTest extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawable_span_test);
        TextView tv = (TextView) findViewById(R.id.text);
        Spannable text = new SpannableStringBuilder("This is liric of the song");
        setSpans(tv, text);
        tv.setText(text);
    }

    private void setSpans(TextView tv, Spannable text) {
        int blueColor = 0xff0000ff;
        int redColor = 0xffff0000;
        ForegroundColorSpan blue = new ForegroundColorSpan(blueColor);
        ForegroundColorSpan red = new ForegroundColorSpan(redColor);
        HalfColorSpan o = new HalfColorSpan("o", tv, blueColor, redColor);
        text.setSpan(blue, 0, 14, 0);
        text.setSpan(o, 14, 15, 0);
        text.setSpan(red, 15, text.length(), 0);
    }

    class HalfColorSpan extends DynamicDrawableSpan {
        private final static String TAG = "DrawableSpanTest.HalfColorSpan";
        Picture mPicture;
        public HalfColorSpan(String text, TextView tv, int c0, int c1) {
            super(ALIGN_BASELINE);
            mPicture = new Picture();
            TextPaint p = tv.getPaint();
            Rect bounds = new Rect();
            p.getTextBounds(text, 0, text.length(), bounds);
            float width = p.measureText(text);
            float height = bounds.height();
            float y = height;
            Canvas c = mPicture.beginRecording((int) width, (int) height);
            c.save(Canvas.CLIP_SAVE_FLAG);
//          c.drawColor(0x22000000);
            p = new TextPaint(p);
            p.setColor(c0);
            c.clipRect(0, 0, width / 2, height, Region.Op.REPLACE);
            c.drawText(text, 0, y, p);
            p.setColor(c1);
            c.clipRect(width / 2, 0, width, height, Region.Op.REPLACE);
            c.drawText(text, 0, y, p);
            c.restore();
            mPicture.endRecording();
        }
        @Override
        public Drawable getDrawable() {
            PictureDrawable d = new PictureDrawable(mPicture);
            d.setBounds(0, 0, mPicture.getWidth(), mPicture.getHeight());
            return d;
        }
    }
}

和布局/ drawable_span_test.xml:

and layout/drawable_span_test.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/text"
    android:textSize="50dip"
    android:gravity="center"
    android:background="#eee"
/>

的结果是:

the result is:

这篇关于突出显示的android文本视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆