2012-06-03 113 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

相關問題