Android TextView的一些常见功能

手机游戏开发者 2024-9-13 22:49:08 96 0 来自 中国
1.png 简介

TextView作为Android开辟中的底子控件,在一样平常开辟中我们频繁使用到TextView的一些功能.这里就简单总结了一下开辟中常用的一些功能
功能


  • 设置巨细不一的笔墨
  • 设置中划下
  • 设置小数点以后笔墨字体巨细
  • 设置差别颜色展示笔墨
  • 展示图文混排
实现

1 设置中划线

  /**     * 中央划线的结果     * @param textView     */    public  static void setStrikethrough(TextView textView){        textView.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);    }2.设置差别笔墨巨细

/**     * Html 方式设置差别笔墨巨细     * @param textview     * @param money     */    public static void setMoney(TextView textview,double money){        String moneyss= "<font color='#FFFFFF'><small>¥ </small></font><font color='#FFFFFF'><big>%s</big></font>";        String moneyContent=String.format(moneyss,getNumDiff(money));        textview.setText(Html.fromHtml(moneyContent));    }    /**     * span 方式设置 差别字体巨细     * @param textview     * @param money     * @param sizeSpan  字体巨细  大于100 是比设置的字体大  小于100是比设置的小     */    public static void setMoney(TextView textview,double money,int sizeSpan){        String moneyss= "¥ %s";        String moneyContent=String.format(moneyss,getNumDiff(money));        SpannableString spannableString = new SpannableString(moneyContent);        spannableString.setSpan(new AbsoluteSizeSpan(sizeSpan), 0, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);        textview.setText(spannableString);    } /**     * 数目转换     */    public  static String getNumDiff(double count) {        String fansNum = "";        if (count >= 0 && count < 10000) {            fansNum = count + "";        } else if (count >= 10000 && count < 10000000) {            if (count / 1000 % 10 != 0) {                float num = (float) count / 10000;                DecimalFormat decimalFormat = new DecimalFormat("0.0");                fansNum = decimalFormat.format(num).concat("万");            } else {                fansNum = (count / 10000) + "万";            }        } else if (count >= 10000000) {            fansNum = "1000万";        }        return fansNum;    }3.设置小数点以后笔墨巨细

/**     * 设置小数点以后笔墨巨细     * @param textview     * @param money     * @param sizeSpan  字体巨细  大于100 是比设置的字体大  小于100是比设置的小     */    public static void setMoneyTextView(TextView textview,double money,int sizeSpan){        String moneyss= "¥ %.2f";        String moneyContent=String.format(moneyss,money);        int index=moneyContent.indexOf(".");        SpannableString spannableString = new SpannableString(moneyContent);        spannableString.setSpan(new AbsoluteSizeSpan(sizeSpan), index, moneyContent.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);        textview.setText(spannableString);    }4.设置差别笔墨展示

    /**     * html方式     * 设置字体的差别颜色     * @param textview     * @param content     */    public static void setColor(TextView textview,String content){//        String str6 = "<font color=\"#00ff00\">我的</font><font color=\"#0000ff\">作业完成了</font>";        String moneyss= "%s <font color=\"#FF0000\">%s</font>";        String strigContent=String.format(moneyss,"我的","作业完成了");        textview.setText(Html.fromHtml(strigContent));    }    /**     * 设置笔墨颜色  span 方式     * @param textview     * @param content     * @param endSize     */    public static void setColor(TextView textview,String content,int endSize){        SpannableString spannableString = new SpannableString(content);        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), endSize, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);        textview.setText(spannableString);    }5.设置图文混排

    /**     * 设置图片     * @param mContext     * @param textview     * @param content     */    public static void setDrawable(Context mContext, TextView textview, String content){        SpannableString spannableString = new SpannableString(" "+content);        //这只图片的宽高        Drawable drawable = mContext.getResources().getDrawable(R.mipmap.ic_launcher);        drawable.setBounds(0, 0, 100, 100);        ImageSpan imageSpan = new ImageSpan(drawable);        spannableString.setSpan(imageSpan, 0, 1, ImageSpan.ALIGN_BASELINE);        Drawable drawable2 = mContext.getResources().getDrawable(R.mipmap.ic_launcher);        drawable2.setBounds(0, 0, 100, 100);        ImageSpan imageSpan2 = new ImageSpan(drawable2);        spannableString.setSpan(imageSpan2, 2, 3, ImageSpan.ALIGN_BASELINE);        textview.setText(spannableString);    }注意
图文混排发起web加载标签方式实现
总结

简单的总结了一下TextView常见的一些展示样式,另有一些处理处罚相对麻烦的,比如扩展TextView  指定范例笔墨高亮展示这里没做睁开.后期继续总结.高亮和扩展的TextView的自定义库放在下边了
资源

高亮和扩展的TextView
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2025-4-10 11:14, Processed in 0.174409 second(s), 35 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表