如何设置不同语言环境的不同字体? 假设我有一个字符串文件hi hindi但我的手机不支持hindi字体,如何从资产中添加字体作为默认字体虽然不在应用程序中?
答案 0 :(得分:0)
要添加自定义字体,请按照以下步骤操作。
3.在您的java文件中添加以下代码
Rational
答案 1 :(得分:0)
首先,将资源文件夹中的自定义字体文件(例如custom.ttf
)复制。
然后,这样做
1)添加此课程OpenHindiFonts.java
import android.content.Context;
import android.graphics.Typeface;
public class OpenHindiFonts {
private static OpenHindiFonts instance;
private static Typeface typeface;
public static OpenHindiFonts getInstance(Context context) {
synchronized (OpenHindiFonts.class) {
if (instance == null) {
instance = new OpenHindiFonts();
typeface = Typeface.createFromAsset(context.getResources().getAssets(), "custom.ttf");
}
return instance;
}
}
public Typeface getTypeFace() {
return typeface;
}
}
2)添加此课程 - NativelyCustomTextView
。
package com.packageName;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class NativelyCustomTextView extends TextView {
public NativelyCustomTextView(Context context) {
super(context);
setTypeface(OpenHindiFonts.getInstance(context).getTypeFace());
}
public NativelyCustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setTypeface(OpenHindiFonts.getInstance(context).getTypeFace());
}
public NativelyCustomTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
setTypeface(OpenHindiFonts.getInstance(context).getTypeFace());
}
}
3)在你的layout xml
中,需要使用它 -
<com.packageName.NativelyCustomTextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hindi_text"
android:textColor="#727272"
android:textSize="18dp" />
4)在您的activity code
中, 无需更改任何内容 -
final TextView textV = (TextView) findViewById(...);
textV.setText(getResources().getString(R.string.hindi_text));
答案 2 :(得分:-1)
@Test limbs:这是我建议的:通过这种方式在活动中创建所需的所有字体:
Typeface tfFonts =Typeface.createFromAsset(getAssets(),"fonts/customfont.ttf");
componentName.setTypeface(tfFonts);
hindi,bangli,你想要什么。创建一个当前的Typeface
变量,并提供一种动态设置它的方法。你需要的只是为你的所有小部件分配你想要的字体字体。 / p>
lefttext.setTypeface((MainActivity.getCurrentTypeface()));
lefttext
是设置字体所需的小部件。
getCurrentTypeface
是您在主活动中创建的方法,用于获取对当前字体的引用。这样您就可以分配所需的所有窗口小部件