2

Android アプリケーション全体のフォントを変更する方法はありますか? 各 TextView と Buttons のフォントを変更することを認識しています。私が取り組んでいるプログラムにはたくさんのレイアウトファイルがあるので、もっとエレガントな方法があるかどうか知りたかっただけです:(

4

3 に答える 3

1

アプリ全体で同じフォント効果を適用するには、カスタム フォントを適用した独自のカスタム TextView および Button クラスを作成する必要があります。レイアウトで通常のビューとして使用します。

public class MinnesotaTextView extends TextView{

    public MinnesotaTextView(Context context) {
        super(context);
        if(!isInEditMode()){
            textViewProprties(context);
        }
    }

    public MinnesotaTextView(Context context, AttributeSet attrs){
        super(context, attrs);
        if(!isInEditMode()){
            textViewProprties(context);
        }
    }

    public MinnesotaTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if(!isInEditMode()){
            textViewProprties(context);
        }
    }

    private void textViewProprties(Context context){
        Typeface tfs = Typeface.createFromAsset(context.getAssets(), "Helvetica.ttf");
        setTypeface(tfs);
        setMaxLines(4);
    }
}

ここにボタンがあります:

public class MinnesotaButton extends Button {

    public MinnesotaButton(Context context){
        super(context);
        if(!isInEditMode()){
            buttonProprties(context);
        }
    }

    public MinnesotaButton(Context context, AttributeSet attrs){
        super(context, attrs);
        if(!isInEditMode()){
            buttonProprties(context);
        }
    }

    public MinnesotaButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if(!isInEditMode()){
            buttonProprties(context);
        }
    }

    private void buttonProprties(Context context){      
        setPadding(0, 4, 0, 0);
        setBackgroundResource(R.drawable.bg_red_btn);
        setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
        setTextSize(13.0f);

        setTextColor(context.getResources().getColor(R.color.white));
        Typeface tfs = Typeface.createFromAsset(context.getAssets(), "garreg.ttf");
        setTypeface(tfs,1);
    }   
}
于 2013-05-28T07:34:38.947 に答える
0

必要な制御の程度に応じて、これを行うには 2 つの方法があります。

1) styles.xml で次のようなカスタム スタイル属性を作成できます。

<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#00FF00</item>
    <item name="android:typeface">monospace</item>
</style>

これはかなり制限されたアプローチであることに注意してください。スタイルには必要なものがすべて含まれているとは限りません。

2) TextView と Button のサブクラスを作成し、スタイリング コードをそれらのコンストラクターに配置できます。必要なカスタム アセットを使用できるため、この方法をお勧めします。(Nasser がこのコード サンプルで私を打ち負かしたのを見たところです。チェックしてみてください - 正しく見えます)

于 2013-05-28T07:36:48.077 に答える