使用字符码 android 检索自定义字符
retrieve custom char with charcode android
我正在使用带有星星、太阳等字符的自定义字体。
我知道他们的字符码是这种字体。
我使用以下代码设置 TextView 字体:
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/myFont.ttf");
setTypeface(tf ,1);
}}
有没有办法,因为我知道他们的字符代码,可以直接在 strings.xml 中以编程方式使用这些特定字符?
您可以通过 java 中的 4 位十六进制代码引用字符:
String someString = "\u2776"
或在资源中:
<string name="some_string_name">\u2776</string>
(2776是❶在默认字体中的代码)
我正在使用带有星星、太阳等字符的自定义字体。
我知道他们的字符码是这种字体。
我使用以下代码设置 TextView 字体:
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/myFont.ttf");
setTypeface(tf ,1);
}}
有没有办法,因为我知道他们的字符代码,可以直接在 strings.xml 中以编程方式使用这些特定字符?
您可以通过 java 中的 4 位十六进制代码引用字符:
String someString = "\u2776"
或在资源中:
<string name="some_string_name">\u2776</string>
(2776是❶在默认字体中的代码)