我可以在数组中“存储”spannable字符串吗?

时间:2012-06-03 10:43:00

标签: android

我需要构建一个包含以下内容的textView: “标题[x]”后跟“一些文字”,后跟“\ n”,后跟“标题[y]”,后跟“更多文字”等,给予

标题[x]一些文字

标题[y]更多文字

我需要标题为粗体,并为每个标题设置不同的颜色。我总共有11个标题和最多30个文本字符串可供选择,任何标题都可以有任何文本字符串。在每个周期中,我的最终结果文本中将包含1到6个标题。 使用x和y的“已知”值构建textView没有问题,但是在现实生活中,我不知道这些值,直到我构建spannable字符串。我不想构建每个可能的字符串变体(超过300)。 我已经尝试创建所有“Title”spannable并将它们添加到我的“结果”中,因为我创建它们并且它工作正常,但是如果我创建它们并在最后将它们附加在一个语句中我会失去Bold和Color属性

我的main.xml有一个textView,其ID为color_test。

package uk.cmj.color;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.TextView.BufferType;

public class ColorActivity extends Activity {
/** Called when the activity is first created. */

private static int titleIndex = 0;
private final String[] titles = {"Title A", "Title B", "Title C"};      

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// ---------------------------------------------
// THIS WORKS AND I SEE COLOR AND BOLD FOR TITLE
// ---------------------------------------------

titleIndex = 1;

SpannableStringBuilder resultText = new SpannableStringBuilder(); 
String firstTitle = titles[titleIndex]; 
SpannableString firstTitleSpannable= new SpannableString(firstTitle); 
firstTitleSpannable.setSpan(new ForegroundColorSpan(Color.rgb(155,79,25)), 0, firstTitle.length(), 0); 
firstTitleSpannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),  0, firstTitle.length(), 0); 
resultText.append(firstTitleSpannable);

String body1 = " some text" + "\n"; 
SpannableString body1Spannable= new SpannableString(body1); 
body1Spannable.setSpan(new ForegroundColorSpan(Color.BLACK), 0, body1.length(), 0); 
resultText.append(body1Spannable); 

titleIndex = 2;

String nextTitle = titles[titleIndex]; 
SpannableString nextTitleSpannable= new SpannableString(nextTitle); 
nextTitleSpannable.setSpan(new ForegroundColorSpan(Color.rgb(155,79,25)), 0, nextTitle.length(), 0); 
nextTitleSpannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),  0, nextTitle.length(), 0); 
resultText.append(nextTitleSpannable + "\n");

String body2 = " some different text" + "\n"; 
SpannableString body2Spannable= new SpannableString(body2); 
body1Spannable.setSpan(new ForegroundColorSpan(Color.BLACK), 0, body1.length(), 0); 
resultText.append(body2Spannable); 

TextView color_test = (TextView) findViewById(R.id.color_test); 
color_test.setText(resultText, BufferType.SPANNABLE); 

// ------------------------------------------
// THIS DOESN'T WORK AS I LOSE COLOR AND BOLD
// ------------------------------------------

titleIndex = 1;

SpannableStringBuilder resultText = new SpannableStringBuilder(); 
String firstTitle = titles[titleIndex]; 
SpannableString firstTitleSpannable= new SpannableString(firstTitle); 
firstTitleSpannable.setSpan(new ForegroundColorSpan(Color.rgb(155,79,25)), 0, firstTitle.length(), 0); 
firstTitleSpannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),  0, firstTitle.length(), 0); 

String body1 = " some text" + "\n"; 
SpannableString body1Spannable= new SpannableString(body1); 
body1Spannable.setSpan(new ForegroundColorSpan(Color.BLACK), 0, body1.length(), 0); 
resultText.append(body1Spannable); 

titleIndex = 2;

String nextTitle = titles[titleIndex]; 
SpannableString nextTitleSpannable= new SpannableString(nextTitle); 
nextTitleSpannable.setSpan(new ForegroundColorSpan(Color.rgb(155,79,25)), 0, nextTitle.length(), 0); 
nextTitleSpannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),  0, nextTitle.length(), 0); 

String body2 = " some different text" + "\n"; 
SpannableString body2Spannable= new SpannableString(body2); 
body1Spannable.setSpan(new ForegroundColorSpan(Color.BLACK), 0, body1.length(), 0); 
resultText.append(body2Spannable); 

resultText.append(firstTitleSpannable 
                + body1Spannable 
                + nextTitleSpannable
                + body2Spannable); 


TextView color_test = (TextView) findViewById(R.id.color_test); 
color_test.setText(resultText, BufferType.SPANNABLE); 

}
}

1 个答案:

答案 0 :(得分:1)

我不太确定我是否正确理解你的问题。但是,如果我这样做,我会写一个包含所有需要的属性的类,并在运行时创建对象:

public class SpannableContent {
    private StringBuilder result = new StringBuilder();
    private String title1 = null;
    private String title2 = null;
    private String description = null;

    public SpannableContent(String _title1, String _title2, String _desc) {
        this.title1 = _title1;
        this.title2 = _title2;
        this.description = _desc;

        result.append(getFormatedTitle1());
        result.append(getFormatedTitle2());
        result.append(getFormatedDescription());
    }

    private String getFormatedTitle1() {
        String resString = null;
        // do whatever Spannable stuff you want to do w/ title1 here

        return resString;
    }

    private String getFormatedTitle2() {
        String resString = null;
        // do whatever Spannable stuff you want to do w/ title2 here

        return resString;
    }

    private String getFormatedDescription() {
        String resString = null;
        // do whatever Spannable stuff you want to do w/ the description here

        return resString;
    }

    public String getFinalContent() {
        return result.toString();
    }
}

然后在运行时做类似

的事情
SpannableContent spannableC = new SpannableContent(dynamicTitle1, dynamicTitle2, dynamicDesc);
resultText.append(spannableC.getFinalContent());

您可以将对象存储在SpannableContent类型的ArrayList中。

ArrayList<SpannableContent> spannableArrayList = new ArrayList<SpannableContent>();