在android中突出显示文本视图

在android中突出显示文本视图,第1张

概述我想在android中突出显示textview与此图像相同我是怎么做到的?谢谢像这个链接http://youtube.com/watch?v=0qE3egNettY解决方法:这就是你想要的:packageorg.pskink.shape;importandroid.app.Activity;importandroid.graphics.Canvas;importandroid.graphics.Rect;import

我想在android中突出显示textvIEw与此图像相同

我是怎么做到的?谢谢

像这个链接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;        }    }}

和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"/>

结果是:

总结

以上是内存溢出为你收集整理的在android中突出显示文本视图全部内容,希望文章能够帮你解决在android中突出显示文本视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1106549.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存