1

アプリにカスタム フォント スタイルを適用しようとしています。

Typeface tf = Typeface.createFromAsset(getAssets(),"font.ttf");
text.setTypeface(tf);

しかし、アクション バーのタブとすべてのテキスト ビューを含むアプリ全体にカスタム フォント スタイルを適用したいと考えています。

アプリ全体にカスタム フォントを適用する「夕食」の方法はありますか??

4

2 に答える 2

0

アプリケーションの書体全体を設定する方法はありませんが、より一般的なプログラム ソリューションを探している場合は、ビュー全体 (アクティビティ UI) の書体を設定するために使用できる静的クラスを作成しました。私は Mono (C#) を使用していますが、Java を使用して簡単に実装できることに注意してください。

このクラスに、カスタマイズしたいレイアウトまたは特定のビューを渡すことができます。非常に効率的になりたい場合は、Singleton パターンを使用して実装できます。

public static class AndroidTypefaceUtility 
{
    static AndroidTypefaceUtility()
    {
    }
    //Refer to the code block beneath this one, to see how to create a typeface.
    public static void SetTypefaceOfView(View view, Typeface customTypeface)
    {
    if (customTypeface != null && view != null)
    {
            try
            {
                if (view is TextView)
                    (view as TextView).Typeface = customTypeface;
                else if (view is Button)
                    (view as Button).Typeface = customTypeface;
                else if (view is EditText)
                    (view as EditText).Typeface = customTypeface;
                else if (view is ViewGroup)
                    SetTypefaceOfViewGroup((view as ViewGroup), customTypeface);
                else
                    Console.Error.WriteLine("AndroidTypefaceUtility: {0} is type of {1} and does not have a typeface property", view.Id, typeof(View));
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("AndroidTypefaceUtility threw:\n{0}\n{1}", ex.GetType(), ex.StackTrace);
                    throw ex;
                }
            }
            else
            {
                Console.Error.WriteLine("AndroidTypefaceUtility: customTypeface / view parameter should not be null");
            }
        }

        public static void SetTypefaceOfViewGroup(ViewGroup layout, Typeface customTypeface)
        {
            if (customTypeface != null && layout != null)
            {
                for (int i = 0; i < layout.ChildCount; i++)
                {
                    SetTypefaceOfView(layout.GetChildAt(i), customTypeface);
                }
            }
            else
            {
                Console.Error.WriteLine("AndroidTypefaceUtility: customTypeface / layout parameter should not be null");
            }
        }

    }

アクティビティでは、Typeface オブジェクトを作成する必要があります。Resources/Assets/ ディレクトリにある .ttf ファイルを使用して OnCreate() で作成します。ファイルがそのプロパティで Android アセットとしてマークされていることを確認してください。

protected override void OnCreate(Bundle bundle)
{               
    ...
    LinearLayout rootLayout = (LinearLayout)FindViewById<LinearLayout>(Resource.Id.signInView_LinearLayout);
    Typeface allerTypeface = Typeface.CreateFromAsset(base.Assets,"Aller_Rg.ttf");
    AndroidTypefaceUtility.SetTypefaceOfViewGroup(rootLayout, allerTypeface);
}
于 2013-08-01T18:59:17.383 に答える