android - 我可以在数组中使用 "store"spannable 字符串吗?

标签 android

我需要构建一个包含以下内容的 textView: "Title[x]"后跟 "some text"后跟 "\n"后跟 "Title[y]"后跟 "more text"等等。给出

标题[x]一些文字

标题[y] 更多文字

我需要标题加粗,并且每个标题的颜色都不同。我总共有 11 个标题和最多 30 个文本字符串可供选择,任何标题都可以有任何文本字符串。在每个周期中,我的最终结果文本中都会有 1 到 6 个标题。 使用 x 和 y 的“已知”值构建 textView 没有问题,但在现实生活中,在构建可跨越字符串之前我不会知道这些值。我不想构建字符串的所有可能变体(超过 300)。 我已经尝试创建所有“Title”spannables 并将它们附加到我的“结果”中,因为我创建它们并且它工作正常,但是如果我创建它们并在最后将它们附加在一个语句中,我会丢失 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); 

}
}

最佳答案

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

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

关于android - 我可以在数组中使用 "store"spannable 字符串吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10869643/

相关文章:

java - 列表中按钮的 onclicklistener

android - 在 Android 中使用 NAT 穿通发送 P2P 消息

android - 如何在不旋转布局的情况下改变方向时旋转按钮?

android - GradleProcessResult.buildProcessException(GradleProcessResult.java:74)

android - dex 在 Android 上为 Scala 构建 Ant 时失败

android - 从 MySql 到 FireBase 的数据结构(NOSQL 数据库)

java - onCreate() 期间静态 TextView 会发生什么

java - 单击时递增 Int

android - 实现类似滑动抽屉的功能

android - 是否可以将 iOS Multipeer Connectivity 与 android 连接?