Android 不同的字体和不同的语言环境?
如何使用不同的语言环境设置不同的字体?Android 不同的字体和不同的语言环境?,android,fonts,locale,Android,Fonts,Locale,如何使用不同的语言环境设置不同的字体? 假设我有一个字符串文件hi-hi-hindi,但我的手机不支持印地语字体,如何通过应用程序将asset中的字体添加为默认字体?要添加自定义字体,请按照以下步骤操作 在“资源”文件夹中创建名为“字体”的文件夹 将自定义字体放入字体文件夹。(您也可以将自定义字体放入资源文件夹) 3.在java文件中添加以下代码 Typeface tfFonts = Typeface.createFromAsset(getAssets(),"fonts/customfont.t
假设我有一个字符串文件hi-hi-hindi,但我的手机不支持印地语字体,如何通过应用程序将asset中的字体添加为默认字体?要添加自定义字体,请按照以下步骤操作
Typeface tfFonts = Typeface.createFromAsset(getAssets(),"fonts/customfont.ttf");
componentName.setTypeface(tfFonts);
首先,复制资产文件夹中的自定义字体文件(例如
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) 在您的布局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" />
@测试肢体:我建议您:通过以下方式在活动中创建所有需要的字体:
Typeface tfFonts =Typeface.createFromAsset(getAssets(),"fonts/customfont.ttf");
componentName.setTypeface(tfFonts);
印地语,孟加拉语,任何你想要的。创建一个当前的Typeface
变量,并提供一种动态设置它的方法。你所需要的只是将你想要的字体分配给你的所有小部件
lefttext.setTypeface((MainActivity.getCurrentTypeface()));
lefttext
是您需要设置字体的小部件。
getCurrentTypeface
是您在主活动中创建的一种方法,用于获取对当前字体的引用。通过这种方式,您可以分配所需的所有小部件,但这只适用于一个id(componentName),我需要在appSecond行中使用它,您必须为文件中的所有可用ID编写。但这只适用于一个TextView,我需要在appany other方法存在的情况下使用它?因为这是一个很长的过程,因为我已经制作了这个应用程序,并且没有给它分配ID…如果我使用字体更好…我发现这是最简单的一个…因为只需要添加两个类并用这个-com.packageName.NativelyCustomTextView(自定义TextView的包名)替换TextView(在布局xmls中),你甚至不需要分配ID。这是可选的@testitbbs
lefttext.setTypeface((MainActivity.getCurrentTypeface()));