3

我需要构建一个 textView,其中包含以下内容:“Title[x]”后跟“some text”,然后是“\n”,然后是“Title[y]”,然后是“more text”等。

标题[x] 一些文字

标题[y] 更多文字

我需要标题为粗体并为每个标题使用不同的颜色。我总共有 11 个标题和多达 30 个文本字符串可供选择,任何标题都可以有任何文本字符串。在每个循环中,我的最终结果文本中都会有 1 到 6 个标题。我用 x 和 y 的“已知”值构建 textView 没有问题,但在现实生活中,在构建可跨字符串之前,我不会知道这些值。我不想构建所有可能的字符串变体(超过 300 个)。我已经尝试创建所有“标题”跨栏,并在创建它们时将它们附加到我的“结果”中,并且效果很好,但是如果我将它们全部创建并在最后将它们附加到一个语句中,我将失去 Bold 和 Color 属性.

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

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); 

}
}
4

1 回答 1

2

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

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());

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

ArrayList<SpannableContent> spannableArrayList = new ArrayList<SpannableContent>();
于 2012-06-03T12:02:12.997 回答