Я решил эту проблему с помощью синглтон-класса.Я даю полный код, чтобы он мог помочь другим.
1. Определите это в XML:
xmlns:lht="http://schemas.android.com/apk/res/com.lht" android:id="@+id/basicLayout"
<com.xyz.util.MidColorTextView
xyz:token="#"
xyz:colorSpan="@color/BrightRed"
xyz:text="@string/AppraisingStaffBottomText_imanage"
style="@style/contentDescriptionText" />
2. Создатькласс MidColorTextView
package com.xyz.util;
public class MidColorTextView extends TextView {
private CharSequence text;
private String token;
private Context context;
private String colorSpan;
private int colorCode;
public MidColorTextView( Context context , AttributeSet attrs ) {
super(context, attrs);
this.context = context;
for(int i = 0; i < attrs.getAttributeCount(); i ++ ) {
// Log.i(TAG, attrs.getAttributeName(i));
/*
* Read value of custom attributes
*/
this.text = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.xyz", "text");
this.token = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.xyz", "token");
this.colorSpan = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.xyz", "colorSpan");
// Log.i("TAG", "token " + token);
// Log.i("TAG", "text " + text);
// Log.i("TAG", "colorSpan " + colorSpan);
}
init();
}
private void init () {
if(text.charAt(0) == '@') {
String tempText = (String) text.subSequence(1, text.length());
this.text = Html.fromHtml(getResources().getString(Integer.parseInt(tempText)));
}
if(token.charAt(0) == '@') {
String tempText = (String) token.subSequence(1, token.length());
this.token = getResources().getString(Integer.parseInt(tempText));
}
if(colorSpan.charAt(0) == '@')
{
String tempText = (String) colorSpan.subSequence(1, colorSpan.length());
this.colorSpan = getResources().getString(Integer.parseInt(tempText));
}
setColorCode(Color.parseColor(colorSpan));
CharSequence textWitoutToken = null;
String tempString = text.toString();
// ---------checking whether text containg token or not.
if(tempString.contains(token))
{
textWitoutToken = setSpanBetweenTokens(text, token, new ForegroundColorSpan(colorCode));
}
else
{
textWitoutToken = text;
}
textContent = null;
setText(textWitoutToken);
setTypeface(FontManager.getInstance(context).getTypefaceArial ());
}
public void setText ( CharSequence text , String token , int color )
{
setText(setSpanBetweenTokens(text, token, new ForegroundColorSpan(color)));
setTypeface(FontManager.getInstance(context).getTypefaceArial ());
}
public int getColorCode ()
{
return colorCode;
}
public void setColorCode ( int colorCode )
{
this.colorCode = colorCode;
}
private CharSequence textContent;
public CharSequence setSpanBetweenTokens ( CharSequence text , String token , CharacterStyle... cs )
{
// Start and end refer to the points where the span will apply
int tokenLen = token.length();
int start = text.toString().indexOf(token) + tokenLen;
int end = text.toString().indexOf(token, start);
if(start > - 1 && end > - 1)
{
// Copy the spannable string to a mutable spannable string
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
for(CharacterStyle c : cs)
{
ssb.setSpan(c, start, end, 0);
}
// Delete the tokens before and after the span
ssb.delete(end, end + tokenLen);
ssb.delete(start - tokenLen, start);
text = ssb;
textContent = ssb;
String tempString = textContent.toString();
if(tempString.contains(token))
{
setSpanBetweenTokens(textContent, token, new ForegroundColorSpan(colorCode));
}
}
return textContent;
}
}
3. Создать класс FontManager
public class FontManager {
private Typeface typefaceArial;
private Context context;
private static FontManager instance = null;
private FontManager(Context context) {
this.context = context;
this.typefaceArial= Typeface.createFromAsset(context.getAssets(), "arial.ttf");
}
public synchronized static FontManager getInstance(Context context) {
if(instance == null) {
instance = new FontManager(context);
}
return instance;
}
public Typeface getTypefaceArial () {
return typefaceArial;
}
}
Это решит все ваши проблемы.
setSpanBetweenTokens
используется для цветного текста между определенными токенами.
Вот строковый ресурс для проверки:
<string name="AppraisingStaffBottomText_imanage">The meeting<br><br>PAST<br>Allow the employee to give you
their view of their positive
progress over the past period, focus them on this with open
questions, such as:<br><i>#\"What has been your important contribution over the past
6
months?\"#</i><br><i>#\"What have your learned about your role?\"#</i><br>
<i>#\"What has been your important success?\"#</i><br>Don\'t rake over past mistakes,
don\'t focus on poor performance
- you cannot change that, reserve those discussions future
development - see below<br><br>PRESENT<br>Using open questions, help staff to identify
their true strengths,
capabilities, attributes, skills and attitudes. Create a
comprehensive picture of them as a strategic contributor and
resource.<br>What are your skills, and to what level?<br><i>#\"What have you added as
capabilities over the past months?\"#</i><br><i>#\"What do you find are your
most useful personal attributes in
your role?\"#</i><br><br>FUTURE<br>The future is the period where changes in
capability and
performance can be made.<br>This discussion is where your people can figure out -
with your
help - what development they need to reach your performance
standards and their career goals. It begins with understanding
their career goals, so ....<br><i>#\"What are your goals?\"#</i><br><i>#\"What
development will be needed?\"#</i>
<br><i>#\"You have seen over the past months that you may need more skill in these
areas .......................... what should we do about
that?\"#</i><br><br>Finally: Agree a specific development plan that includes
training/experience in the areas where more skill is needed. Fix dates
in the diary<br><br>The Manager\'s role is one of Mentor and Guide; not
Judge and
Jury</string>