Dan*_*yer 167
我没有尝试过这个以确保它确实可行,但在理想的世界中TextView
应该尊重Unicode不间断空格字符(\u00A0
),这将是比HTML更简单/更轻的解决方案.
TWi*_*Rob 20
可以使用
具有可读解决方案.除非您记住十六进制代码,否则在文本中包含\u00A0
或者/  
或 
/  
并没有真正向源代码(或翻译者)的读者传达太多信息.这是一种在以下方面使用命名实体的方法strings.xml
:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
<!ENTITY nbsp " "><!-- non-breaking space, U+00A0 -->
]>
<resources>
...
</resources>
Run Code Online (Sandbox Code Playgroud)
这将创建缺少的声明.最初的HTML声明可以在https://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent中找到,它来自通常的XHTML DTD.所有这一切都有效,因为XML解析器在加载文件时读取这些并替换它们,因此实体将不会出现在生成的编译资源中.
在Android Text(CharSequence
)资源中<!-- Defined in <resources> -->
<string name="html_text">Don\'t break <b>this name</b></string>
<!-- Used in a layout -->
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:background="#10000000"
android:text="@string/html_text"
/>
Run Code Online (Sandbox Code Playgroud)
在Android String(格式化)资源中<!-- Defined in <resources> -->
<string name="formatted_text">%1$s is nice</string>
<!-- Used in a layout -->
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:background="#10000000"
tools:text="@string/formatted_text"
/>
Run Code Online (Sandbox Code Playgroud)
然后在代码中:
String contents = getString(R.string.formatted_text, "Using an ");
((TextView)view.findViewById(android.R.id.text1)).setText(contents);
Run Code Online (Sandbox Code Playgroud)
这些只是DTD实体的示例用法,根据您自己的喜好使用它.
<!ENTITY con "\'"><!-- contraction, otherwise error: "Apostrophe not preceded by \"
Sadly ' cannot be overridden due to XML spec:
https://www.w3.org/TR/xml/#sec-predefined-ent -->
<!ENTITY param1 "%1$s"><!-- format string argument #1 -->
<string name="original">Don\'t wrap %1$s</string>
<string name="with_entities">Don&con;t wrap ¶m1;</string>
Run Code Online (Sandbox Code Playgroud)
rds*_*rds 18
TextView应该尊重不间断的空间
<string name="test">Hello world</string>
Run Code Online (Sandbox Code Playgroud)
要么
new TextView("Hello\u00A0world");
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
37011 次 |
最近记录: |