グジャラート語のニュース アプリケーションを開発しています。今私の問題は、うまく機能していることですが、フォントの代わりに Squares([]) が表示されるため、グジャラート語で表示してグジャラート語のフォントを表示するにはどうすればよいですか。
前もって感謝します。
グジャラート語のニュース アプリケーションを開発しています。今私の問題は、うまく機能していることですが、フォントの代わりに Squares([]) が表示されるため、グジャラート語で表示してグジャラート語のフォントを表示するにはどうすればよいですか。
前もって感謝します。
まずアセットフォルダにフォントをコピーしてから書き込みます
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/gujaratifont.otf");
その後 :
text.setTypeFace(tf);
xmlを使ってフォントの書体を変更する方法です。
新しい NameSpace を定義します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>
TextView を拡張する Java クラス... 次のように public class TypefacedTextView extends TextView {
public TypefacedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
//Typeface.createFromAsset doesn't work in the layout editor. Skipping...
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" />
それだけで十分です...