2012-06-03 81 views
2

我有一个要求,建立一个文本视图,其中包含类似于: “标题[x]”后面跟着“一些文本”,后面跟着“\ n”,接着是“标题[y]”,后跟“更多文本”等。给我可以“存储”一个数组中的spannable字符串吗?

题名[X]一些文本

题名[Y]更多的文字

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

我main.xml中有color_test的ID一个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); 

} 
} 

回答

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

感谢您的支持。我实际上是通过为每一行创建一个具有RGB数字的数组来解决我的问题,这些数据完成了这项工作。我对android/java非常陌生,因此所有的解决方案对我都很有用。目前,开展工作比有效地开展工作更重要 - 我将最终走上舞台! ArrayList 现在位于我要调查的项目列表中! – Marvin

相关问题