现在的位置: 首页 > 综合 > 正文

采用匹配规则来填充String.xml中的资源

2013年06月28日 ⁄ 综合 ⁄ 共 716字 ⁄ 字号 评论关闭

第一种方案:1.在String.xml文件中申明 string内容如   <string name="format_error">请使用%1$s:%2$s:%3$s的格式</string>
              2.引用该资源的时候为:  String  text=resources.getString(R.string.hello, "xxx");
        3.在有通配符下,string节点中设置  formatted="false"  这些特殊的字符% <string  formatted="false"  name="hello">%1$ssHello%2$ssworld%3$s %Hello world!</string>
           那么这些单独的字符就被视为 一些字符:如%就会写成385DB8,也有可能抛异常。
  第二种方案:
            1.在String.xml中申明:string节点:<string name="is_hall_common_summary">第ISSUE期 还有TIME停售</string>
            2.引用资源:String text = context.getResources().getString(R.string.is_hall_common_summary);
          3.采用org.apache.commons.lang3.StringUtils中的replaceEach();
         text = StringUtils.replaceEach(text, new String[]{"xxx","zzz"}, new String[]{text1,text2});

抱歉!评论已关闭.