Android系统。如何在TextView中使用符号代码?

时间:2015-04-09 10:18:39

标签: android css typeface android-typeface

我使用自定义字体显示天气状况(晴天,下雨,下雪......)。我正在引用assets / fonts文件夹中的字体。

Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/Climacons.ttf");
TextView tvTest = (TextView)findViewById(R.id.tvTest);
tvTest.setTypeface(myTypeface);
tvTest.setText(getString(R.string.clear_day));

clear_day字符串包含'sun'符号的代码。

<string name="clear_day">\e028</string>

我从它的CSS中获取了这段代码:

Pseudo ::before element
.climacon.sun:before {
  content: "\e028";
}

但TextView正在显示其他符号

enter image description here

所以我的问题是如何使用代码在TextView中显示符号?

1 个答案:

答案 0 :(得分:0)

我用这个:

 //function
 private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
 public static Typeface getFace(Context c, String assetPath) {
    synchronized (cache) {
        if (!cache.containsKey(assetPath)) {
            try {
                Typeface t = Typeface.createFromAsset(c.getAssets(),
                        assetPath);
                cache.put(assetPath, t);
            } catch (Exception e) {
                Log.e(TAG, "Could not get typeface '" + assetPath
                        + "' because " + e.getMessage());
                return null;
            }
        }
        return cache.get(assetPath);
    }
}

//where you use
yourView.setTypeface(getFace(getActivity().getApplicationContext(),"counterfont.ttf"));

将* .ttf放在assets文件夹中(assets不在res文件夹中!)

然后将您的图标字符串放在yourView xml内,在我的情况下,“i”是邮件图标:

<TextView
          ...
          android:text="i"
          ...
           />