热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android-查找子字符串并将其设为粗体

如何解决《Android-查找子字符串并将其设为粗体》经验,为你挑选了1个好方法。

找到str1作为str2的子字符串,使其变为粗体(即在str2中),然后将其显示为textview的正确方法是什么?



1> Ferdous Aham..:

使用str2SpannableString和使用setSpan()方法找到子串str1,使之BOLD

请参阅文档。

尝试这个:

    TextView textView = (TextView) findViewById(R.id.textView);

    String str1 = "substring";
    String str2 = "Find a substring and make it bold";

    if (str2.contains(str1)) {

        SpannableString spannable = new SpannableString(str2);
        spannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 
                str2.indexOf(str1), 
                str2.indexOf(str1) + str1.length(), 
                Spannable.SPAN_INCLUSIVE_INCLUSIVE);

        textView.setText(spannable);
    } else {
        textView.setText(str2);
    }

输出:

希望这会有所帮助〜


推荐阅读
author-avatar
手机用户2602926907
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有