Android如何着色字符串的特定部分

文章选自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。(#换成@)

posted @ 2014-01-14 13:39  一个学渣  阅读(529)  评论(0编辑  收藏  举报

 

CSDNGitHub 联系邮箱:aikongmeng@gmail.com


Copyright 2008-2019 Government All Rights Reserved