我正在开发一款古吉拉特语新闻应用程序。目前遇到的问题是,应用程序运行正常,但显示的是方块([]),而不是字体。请问如何使其在古吉拉特语中可见,以便显示古吉拉特语字体?
提前感谢您的帮助。
提前感谢您的帮助。
首先复制字体到资产文件夹中,然后编写:
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/gujaratifont.otf");
text.setTypeFace(tf);
这是使用xml更改字体类型的方法。
定义一个新的命名空间:xmlns:your_namespace="http://schemas.android.com/apk/res/com.nound.test"
(“com.nound.test”是在Manifest.xml中定义的包名)
在/res/values/attrs.xml文件中定义自定义属性。
<resources>
<declare-styleable name="TypefacedTextView">
<attr name="textStyle" format="string"/>
</declare-styleable> </resources>
Java类继承TextView...如下所示 public class TypefacedTextView extends TextView {
public TypefacedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
//Typeface.createFromAsset在布局编辑器中无法使用。跳过...
if (isInEditMode()) {
return;
}
TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TypefacedTextView);
//String fontName = styledAttrs.getString(R.styleable.TypefacedTextView_typeface);
String fontNameForBold = styledAttrs.getString(R.styleable.TypefacedTextView_textStyle);
styledAttrs.recycle();
if (fontNameForBold!=null && fontNameForBold.equals("bold")) {
Typeface typeface_bold = Typeface.createFromAsset(context.getAssets(), "fonts/tahoma/tahoma_bold.ttf");
setTypeface(typeface_bold);
}else{
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/tahoma/tahoma.ttf");
setTypeface(typeface);
}
}
这是你想要更改字体类型的布局代码。在这里,“com.nound.test.ui”是上面(第3点)java文件的包名。TypefacedTextView是类名。
<com.nound.test.ui.TypefacedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:your_namespace="http://schemas.android.com/apk/res/com.nound.test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Custom fonts in XML are easy_bold font"
your_namespace:textStyle="bold" />
<com.nound.test.ui.TypefacedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:your_namespace="http://schemas.android.com/apk/res/com.nound.test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Custom fonts in XML are easy_regular font"
/>
只需要做...