gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:04:24 24 4
gpt4 key购买 nike

我需要构建一个包含以下内容的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com