Android中富文本的使用

其實(shí)主要就是給SpannableStringBuilder 加上各種setSpan讓其實(shí)現(xiàn)不同的展示方式

/**
     * 初始化富文本
     */
    private void initRichText() {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("注冊即表示你閱讀并同意《興籌網(wǎng)平臺用戶協(xié)議》");
        //下劃線
        UnderlineSpan underlineSpan = new UnderlineSpan();
        //設(shè)置字體北京顏色
        BackgroundColorSpan colorSpan = new BackgroundColorSpan(R.color.green2);
        //設(shè)置字體
        StyleSpan styleSpan = new StyleSpan(Typeface.BOLD_ITALIC);
        //設(shè)置字體大小
        AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan(BIND_ABOVE_CLIENT);

        spannableStringBuilder.setSpan(new URLSpan("http://www.xingchouwangluo.com/webapp/yonghuxieyi.html") {
            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(getResources().getColor(R.color.themeColor));
//                ds.setUnderlineText(true);
            }
        }, 11, 21, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

        //方法重新設(shè)置文字背景為透明色。
        tv_regist_argeement.setHighlightColor(getResources().getColor(android.R.color.transparent));
        tv_regist_argeement.setMovementMethod(LinkMovementMethod.getInstance());
        tv_regist_argeement.setText(spannableStringBuilder);
    }

setSpan里邊可加的屬性大全

        1、BackgroundColorSpan 背景色
        2、ClickableSpan 文本可點(diǎn)擊,有點(diǎn)擊事件
        3、ForegroundColorSpan 文本顏色(前景色)
        4、MaskFilterSpan 修飾效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)
        5、MetricAffectingSpan 父類,一般不用
        6、RasterizerSpan     光柵效果
        7、StrikethroughSpan  刪除線(中劃線)
        8、SuggestionSpan 相當(dāng)于占位符
        9、UnderlineSpan      下劃線
        10、AbsoluteSizeSpan      絕對大小(文本字體)
        11、DynamicDrawableSpan      設(shè)置圖片,基于文本基線或底部對齊。
        12、ImageSpan         圖片
        13、RelativeSizeSpan         相對大?。ㄎ谋咀煮w)
        14、ReplacementSpan      父類,一般不用
        15、ScaleXSpan        基于x軸縮放
        16、StyleSpan           字體樣式:粗體、斜體等
        17、SubscriptSpan         下標(biāo)(數(shù)學(xué)公式會用到)
        18、SuperscriptSpan       上標(biāo)(數(shù)學(xué)公式會用到)
        19、TextAppearanceSpan       文本外貌(包括字體、大小、樣式和顏色)
        20、TypefaceSpan       文本字體
        21、URLSpan 文本超鏈接
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容