文章选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术。本文探讨Android如何着色字符串的特定部分。

问题:

SiKni8

如下CustomAdapter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.test.testing;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;
 
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
public class SetRowsCustomAdapter extends ArrayAdapter<SetRows> {
    Context context;
   int layoutResourceId;
   ArrayList<SetRows> data=new
ArrayList<SetRows>();
   DateFormat df = new
SimpleDateFormat("EEEEE, LLLL d", Locale.US);
   String[] suspendedDates = {
            "Monday, January 20",
            "Friday, January 31",
    };
   public SetRowsCustomAdapter(Context context, int layoutResourceId, ArrayList<SetRows> data) {
       super(context, layoutResourceId, data);
       this.layoutResourceId = layoutResourceId;
       this.context = context;
       this.data = data;
   }
 
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
       View row = convertView;
       ImageHolder holder = null;
 
       if(row ==
null)
       {
           LayoutInflater inflater = ((Activity)context).getLayoutInflater();
           row = inflater.inflate(layoutResourceId, parent,
false);
 
           holder = new
ImageHolder();
           holder.txtTitle = (TextView)row.findViewById(R.id.tvDateVal);
           //holder.txtTitle.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/robm.ttf"));
           holder.imgIcon = (ImageView)row.findViewById(R.id.ivIcon0);
           holder.txtDate = (TextView)row.findViewById(R.id.tvDateNum);
           holder.txtID = (TextView)row.findViewById(R.id.tvReasonVal);
           //holder.txtID.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/robm.ttf"));
           row.setTag(holder);
       }
       else
       {
           holder = (ImageHolder)row.getTag();
       }
 
       SetRows myImage = data.get(position);
       int inReason = myImage.name.indexOf(",");
//myImage.name is the same string as suspendedDates[];
        String strR = myImage.name.substring(0, inReason);
        Spannable WordToSpan = new
SpannableString(strR);
        WordToSpan.setSpan(new
ForegroundColorSpan(Color.parseColor("#4787ED")), 0, WordToSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        String strRNext = myImage.name.substring(inReason, myImage.name.length());
        Spannable WordToSpan1 = new
SpannableString(strRNext);
    WordToSpan1.setSpan(new
ForegroundColorSpan(R.color.dateholiday), 0, WordToSpan1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        String strConcat = WordToSpan.toString() + WordToSpan1.toString();
 
       holder.txtTitle.setText(strConcat);//myImage.name);
       holder.txtID.setText(myImage.id);
       holder.txtDate.setText(myImage.date);
       int outImage=myImage.image;
       /*if (myImage.name.contains(df.format(Calendar.getInstance(Locale.US).getTime()))) {
           holder.imgIcon.setImageResource(R.drawable.caliconpressed);
       }
       else {
           holder.imgIcon.setImageResource(R.drawable.calicon);
       }*/
      return
row;
 
   }
 
   static class ImageHolder
   {
       ImageView imgIcon;
       TextView txtTitle;
       TextView txtID;
       TextView txtDate;
   }
}

我用Spannable来给字符串分别着色,想要达到这种效果

但是显示出来的还是这个样子:

有没有谁知道如何利用Adapter来实现我想要的效果。

答案:

Booger

不要把Spannable变换成字符串 (也就是说,不要做:WordToSpan.toString())

而是,直接将Spannable设置到holder,就像这样:

1
holder.txtTitle.setText(WordToSpan + WordToSpan1);

Martin Cazares

像下面这样用spannable 字符串

1
2
3
4
SpannableString ss = new
SpannableString("hey #abc how are you.");
ss.setSpan(new
ForegroundColorSpan(Color.RED), 4, 9, 0);
//Now just add the SpannableString to your textview
textView.setText(ss);

希望这能帮到你。

原文链接:How to color specific part of a String

文章选自StackOverFlow社区,鉴于其内容对于开发者有所帮助,现将文章翻译于此,供大家参考及学习。9Tech将每日持续更新,读者可点击StackOverflow(简称:SOF)精选问答汇总,查看全部译文内容。同时,我们也招募志同道合的技术朋友共同翻译,造福大家!报名请发邮件至zhangqi_wj#cyou-inc.com。(#换成@)

Android如何着色字符串的特定部分的更多相关文章

  1. SQL:将字符串以特定字符分割并返回Table

    split 语法 ALTER FUNCTION [dbo].[F_SPLIT] ( @str VARCHAR(MAX) , ) ) /********************************* ...

  2. 题目1049:字符串去特定字符——九度OJ

    题目1049:字符串去特定字符 http://ac.jobdu.com/problem.php?pid=1049 时间限制:1 秒 内存限制:32 兆 题目描述: 输入字符串s和字符c,要求去掉s中所 ...

  3. Android状态栏着色

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 状态栏着色,也就是我们经常听到的沉浸式状态栏,关于沉浸式的称呼网上也有很多吐槽的,这里就不做过多讨论了,以下我们统称状态栏着色,这样 ...

  4. Android中五大字符串总结(String、StringBuffer、StringBuilder、Spanna

    https://www.aliyun.com/jiaocheng/2861.html?spm=5176.100033.1.35.2ed56b03CbsYFK 摘要:String.StringBuffe ...

  5. 九度oj 题目1049:字符串去特定字符

    题目1049:字符串去特定字符 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:10173 解决:4611 题目描述: 输入字符串s和字符c,要求去掉s中所有的c字符,并输出结果. 输入: ...

  6. C语言考题:输入一个字符串,将此字符串中特定的字符删去后, 显示新的字符串,要求用函数来完成删去字符的操作。

    #include <stdio.h> #include <string.h> /*此题只需要删除单个字符,比较简单.相信大家也能做出来的.我这个也是可以实现的.只是加了两个判断 ...

  7. Android InputStream接收 字符串乱码 问题

    各个国家和地区所制定的不同 ANSI 编码标准中,都只规定了各自语言所需的“字符”.比如:汉字标准(GB2312)中没有规定韩国语字符怎样存储.这些 ANSI 编码标准所规定的内容包含两层含义:1. ...

  8. JS正则表达式获取字符串中特定字符

    JS正则表达式获取字符串中得特定字符,通过replace的回调函数获取. 实现的效果:在字符串中abcdefgname='test'sddfhskshjsfsjdfps中获取name的值test  实 ...

  9. Android开发_字符串处理类-TextUtils类

    对于字符串处理Android为我们提供了一个简单实用的TextUtils类,如果处理比较简单的内容不用去思考正则表达式不妨试试这个在android.text.TextUtils的类,主要的功能如下: ...

随机推荐

  1. Django异常问题之Error: [WinError 10013] 以一种访问权限不允许的方式做了一个访问套接字的尝试。

    一般情况下,我们启动Django项目时默认设置的端口为8000,当你听着酷狗音乐敲着代码,启动Django项目时忽然翻车了. 不要慌,那是酷狗抢先一步占用了8000端口,解决这个问题的方式就是修改端口 ...

  2. 解决ERR Client sent AUTH, but no password is set

    在搭建cookies池时,需要将账号密码保存到redis,保存时报错:ERR Client sent AUTH, but no password is set 报错原因:Redis服务器没有设置密码, ...

  3. PAT 7-14 公路村村通

    https://pintia.cn/problem-sets/1111189748004499456/problems/1111189831248850957 现有村落间道路的统计数据表中,列出了有可 ...

  4. JEECG & JEESite Tomcat集群 Session共享

    多台tomcat服务的session共享 memcached与redis - JEECG开源社区 - CSDN博客https://blog.csdn.net/zhangdaiscott/article ...

  5. 获取环境变量,0x000000cb 操作系统找不到已输入的环境选项

    include "stdafx.h" #include <Windows.h> #include <iostream> #pragma warning(di ...

  6. maven eclipse 第3方包

    C:\Users\3510\.m2\repository\myjar install:install-file -Dfile=C:\Users\3510\.m2\repository\myjar\al ...

  7. leetcode:Roman to Integer and Integer to Roman

    2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...

  8. html 引入页面公共部分(header、footer)

    html引入页面的公共部分,比如导航栏啊,页头页脚之类的. 1.将需要引入的公共html部分转换为js文件,这里推荐一个转换工具地址 http://tool.chinaz.com/Tools/Html ...

  9. javascript深入浅出——学习笔记(六种数据类型和隐式转换)

    在慕课之前学过JS深入浅出,最近发现很多东西都记不太清楚了,再复习一遍好了,感觉这个课程真的超级棒的,做做笔记,再添加一些学习内容

  10. java 中Excel的导入导出

    部分转发原作者https://www.cnblogs.com/qdhxhz/p/8137282.html雨点的名字  的内容 java代码中的导入导出 首先在d盘创建一个xlsx文件,然后再进行一系列 ...