string资源中的HTML?
我知道我可以把string资源中的转义HTML标签。 但是,查看联系人应用程序的源代码,我可以看到他们有一种不必编码HTML的方法。 从联系人应用程序strings.xml中引用:
<string name="contactsSyncPlug"><font fgcolor="#ffffffff">Sync your Google contacts!</font> \nAfter syncing to your phone, your contacts will be available to you wherever you go.</string>
不幸的是,当我尝试类似的东西(如Hello, <b>World</b>!
)时, getString()
返回没有标签的string(我可以在logcat
看到)。 这是为什么? 我怎样才能得到原来的string,标签和一切? 联系人应用程序如何执行此操作?
你也可以将你的html包围在CDATA块中,getString将返回你的实际的HTML。 像这样:
<string name="foo"><![CDATA[Foo Bar <a href="foo?id=%s">baz</a> is cool]]></string>
现在当你执行一个getString(R.string.foo)时,这个string将是HTML。 如果您需要通过可点击的TextView呈现HTML(如图所示的链接),您需要执行Html.fromHtml(…)调用以获取可跨越的文本。
看来getString()
就是这样做的 – 获取一个string 。 要使用这个,你必须使用getText()
(不再有Html.fromHtml()
),即:
mTextView.setText(getText(R.string.my_styled_text));
但是,似乎android:text
属性只是一样的东西,下面是等价的:
<TextView android:text="@string/my_styled_text" />
在strings.xml
:
<string name="my_styled_text">Hello, <b>World</b>!</string>
最好的解决scheme是以一种方式使用资源:
<string name="htmlsource"><![CDATA[<p>Adults are spotted gold and black on the crown, back and wings. Their face and neck are black with a white border; they have a black breast and a dark rump. The legs are black.</p><p>It is similar to two other golden plovers, Eurasian and Pacific. <h1>The American Golden Plover</h1> is smaller, slimmer and relatively longer-legged than Eurasian Golden Plover (<i>Pluvialis apricaria</i>) which also has white axillary (armpit) feathers. It is more similar to Pacific Golden Plover (<i>Pluvialis fulva</i>) with which it was once <b>considered</b> conspecific under the name \"Lesser Golden Plover\". The Pacific Golden Plover is slimmer than the American species, has a shorter primary projection, and longer legs, and is usually yellower on the back.</p><p>These birds forage for food on tundra, fields, beaches and tidal flats, usually by sight. They eat insects and crustaceans, also berries.</p>]]></string>
并显示它与:
Spanned sp = Html.fromHtml( getString(R.string.htmlsource)); tv.setText(sp);
尝试使用该资源没有和tv.setText(getText(R.string.htmlsource)); 你会看到不同之处
想法:将HTML格式化为JSON格式的文件并将其存储在/ res / raw目录中。 (JSON不那么挑剔)
像这样将数据logging存储在数组对象中:
[ { "Field1": "String data", "Field2": 12345, "Field3": "more Strings", "Field4": true }, { "Field1": "String data", "Field2": 12345, "Field3": "more Strings", "Field4": true }, { "Field1": "String data", "Field2": 12345, "Field3": "more Strings", "Field4": true } ]
要读取您的应用程序中的数据:
private ArrayList<Data> getData(String filename) { ArrayList<Data> dataArray = new ArrayList<Data>(); try { int id = getResources().getIdentifier(filename, "raw", getPackageName()); InputStream input = getResources().openRawResource(id); int size = input.available(); byte[] buffer = new byte[size]; input.read(buffer); String text = new String(buffer); Gson gson = new Gson(); Type dataType = new TypeToken<List<Map<String, Object>>>() {}.getType(); List<Map<String, Object>> natural = gson.fromJson(text, dataType); // now cycle through each object and gather the data from each field for(Map<String, Object> json : natural) { final Data ad = new Data(json.get("Field1"), json.get("Field2"), json.get("Field3"), json.get("Field4")); dataArray.add(ad); } } catch (Exception e) { e.printStackTrace(); } return dataArray; }
最后, Data
类只是容易访问的公共variables的容器…
public class Data { public String string; public Integer number; public String somestring; public Integer site; public boolean logical; public Data(String string, Integer number, String somestring, boolean logical) { this.string = string; this.number = number; this.somestring = somestring; this.logical = logical; } }
它适用于我没有CDATA块。
<string name="menu_item_purchase" translatable="false"><font color="red">P</font><font color="orange">r</font><font color="yellow">e</font><font color="green">m</font><font color="white">i</font><font color="blue">u</font><font color="purple">m</font></string>`enter code here`
我在布局中使用它。
<item android:id="@+id/nav_premium" android:icon="@drawable/coins" android:title="@string/menu_item_purchase" />