代码之家  ›  专栏  ›  技术社区  ›  Felix

字符串资源中的HTML?

  •  106
  • Felix  · 技术社区  · 15 年前

    我知道我可以在字符串资源中放置转义的HTML标记。但是,查看contacts应用程序的源代码,我可以看到它们有一种不必编码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() 返回不带标记的字符串(我可以在 logcat )为什么?我怎样才能得到原始的字符串、标签和所有东西?联系人应用程序是如何完成的?

    5 回复  |  直到 8 年前
        1
  •  170
  •   Donn Felker    14 年前

    您也可以将HTML包围在一个CDATA块中,getString将返回实际的HTML。就像这样:

    <string name="foo"><![CDATA[Foo Bar <a href="foo?id=%s">baz</a> is cool]]></string>
    

    现在,当您执行getstring(r.string.foo)时,字符串将是html。如果需要通过可点击的文本视图呈现HTML(链接如图所示),则需要执行html.fromhtml(…)调用以获取可点击的文本。

        2
  •  82
  •   Felix    15 年前

    看起来 getString() 就这样——得到一个 一串 . 要使用这个,你必须使用 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>
    
        3
  •  46
  •   Felix    13 年前

    最好的解决方案是以某种方式使用资源:

    <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(gettex(r.string.htmlsource))的情况下使用该资源,您将看到不同之处。

        4
  •  0
  •   Someone Somewhere    11 年前

    想法:将HTML放在JSON格式的文件中,并将其存储在/res/raw中。(JSON不那么挑剔)

    将这样的数据记录存储在数组对象中:

    [
        {
            "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 类只是一个公共变量容器,便于访问…

    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;
        }
    }
    
        5
  •  0
  •   Andrew Glukhoff    8 年前

    它对我来说没有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"
        />