我正在做Firebase RemoteConfig集成。在其中一个场景中,我需要断开文本行,所以我尝试使用换行符 ( n
(。
但这不起作用,它既不显示为额外字符,也不创建另一行。
我的解决方案是手动替换n
(假设在 Firebase 控制台中将 TITLE 的属性设置为"TitleNewLine"(:
FirebaseRemoteConfig.getInstance().getString(TITLE).replace("\n", "n")
尝试使用不常见的字符,如两个管道 || 然后在代码中执行 getString(( 后用换行符替换这些字符的每个出现。
诀窍(实际上适用于目标平台上支持的所有 HTML 标记(是将字符串包装在 RemoteConfig 上的 JSON 对象中,如下所示:
{
"text":"Your text with linebreaks...<br><br>...as well as <b>bold</b> and <i>italic</i> text.
}
在目标平台上,您需要解析 JSON 并将其转换回简单字符串。在安卓上,这看起来像这样:
// extract value from JSON
val text = JSONObject(remoteConfig.getString("remoteConfig_key")).getString("text")
// create Spanned and use it
view.text = HtmlCompat.fromHtml(text)
您可以将编码文本(使用 Base64(插入到 Firebase 面板中。
之后,从 Java 类中解码字符串并使用它。
喜欢
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");
所以对我有用的是使用"||"(或您确信不会在字符串中的其他字符组合(作为换行符。然后改为"||"和""。然后,此字符串将为我正确显示。
由于某种原因,在字符串中发送""未按预期被识别,但在接收端手动添加它似乎有效。
要提出上述建议,您可以尝试此代码(可以推广为"n"个元素(。只需将示例文本替换为具有相同格式的示例文本并添加元素数量
String text="#Elemento1#Elemento2#Elemento3#";
int cantElementos=3;
arrayElementosFinales= new String[cantElementos];
int posicionNum0=0;
int posicionNum1;
int posicionNum2;
for(int i=0;i<cantElementos;i++){
posicionNum1=text.indexOf("#",posicionNum0);
posicionNum2=text.indexOf("#", posicionNum1+1);
char [] m = new char[posicionNum2-posicionNum1-1];
text.getChars(posicionNum1+1, posicionNum2,m,0);
arrayElementosFinales[i]=String.valueOf(m);
posicionNum0=posicionNum2;
}
在远程配置中使用 Cdata,结合使用 "br" 标签和 HTML.fromHtml(( .. 例如。
<![CDATA[ line 1<br/>line 2]]>