3

グジャラート語のニュース アプリケーションを開発しています。今私の問題は、うまく機能していることですが、フォントの代わりに Squares([]) が表示されるため、グジャラート語で表示してグジャラート語のフォントを表示するにはどうすればよいですか。

前もって感謝します。

4

2 に答える 2

4

まずアセットフォルダにフォントをコピーしてから書き込みます

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/gujaratifont.otf");

その後 :

text.setTypeFace(tf);
于 2012-03-29T11:02:47.340 に答える
0

xmlを使ってフォントの書体を変更する方法です。

  1. 新しい NameSpace を定義しますxmlns:your_namespace="http://schemas.android.com/apk/res/com.nound.test"(「com.nound.test」は Manifest.xml で定義されたパッケージ名です)。

  2. /res/values/attrs.xml ファイルでカスタム属性を定義します。

    <resources>
        <declare-styleable name="TypefacedTextView">
             <attr name="textStyle" format="string"/>
        </declare-styleable> </resources>
    
  3. 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);
        }
    }
    
  4. フォントの書体を変更するレイアウト コードを次に示します。この「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"
            />

それだけで十分です...

于 2012-03-29T13:18:23.473 に答える