如何为整个应用程序更改我的大小字体?

时间:2015-02-18 17:32:57

标签: java android

在我的应用程序中,我有一个名为“font size”的菜单 我想让用户有权为整个应用选择合适的尺寸。 例如3种尺寸。小号中号大号。 当用户选择其中一种尺寸时,整个应用程序必须更改为新尺寸。 我搜索了很多,但是有没有可以帮助我的样本或链接? plz建议你知道的任何样本。 我也有这些代码来更改字体,它很容易工作。 如何进行更改才能使用我的字体大小。

enter code here
public class G extends Application {

public static Context           context;
public static String            defaultFontName;
public static Typeface          defaultFont;
public static SharedPreferences preference;
public static Activity          currentActivity;


@Override
public void onCreate() {
    super.onCreate();
    context = getApplicationContext();
    preference = getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
    defaultFontName = preference.getString("default_font", "b_koodak.ttf");
    defaultFont = Typeface.createFromAsset(getAssets(), "fonts/" + defaultFontName);

}

}

公共类TestFontActivity扩展了Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    G.currentActivity = this;
    setContentView(R.layout.main);

    TextView txtFont = (TextView) findViewById(R.id.txtFont);
    txtFont.setTypeface(G.defaultFont);
    Button btnFont = (Button) findViewById(R.id.button1);
    btnFont.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            showSelectFontDialog();
        }
    });
}


public void showSelectFontDialog() {

    final Dialog dialog1 = new Dialog(G.currentActivity);

    dialog1.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    dialog1.setContentView(R.layout.dialog_font);

    ArrayList<RadioButton> radioBtns1 = new ArrayList<RadioButton>();
    radioBtns1.add((RadioButton) dialog1.findViewById(R.id.radioFontTitr));
    radioBtns1.add((RadioButton) dialog1.findViewById(R.id.radioFontKoodak));
    radioBtns1.add((RadioButton) dialog1.findViewById(R.id.radioFontNazanin));

    for (final RadioButton radio: radioBtns1) {

        Typeface tff = Typeface.createFromAsset(G.context.getAssets(), "fonts/" + ((String) radio.getTag()));
        radio.setTypeface(tff);
        if (((String) radio.getTag()).equals(G.defaultFontName)) {
            radio.setChecked(true);
        }
        radio.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                G.defaultFontName = (String) radio.getTag();
                SharedPreferences.Editor editor = G.preference.edit();
                editor.putString("default_font", G.defaultFontName);
                editor.commit();

                G.defaultFont = Typeface.createFromAsset(G.context.getAssets(), "fonts/" + G.defaultFontName);

                Toast.makeText(getApplicationContext(), "you can see the changes in your font", 1).show();
                dialog1.dismiss();
            }
        });

    }

    dialog1.show();

}

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >


<LinearLayout
    android:layout_width="270dip"
    android:layout_height="wrap_content"
    android:orientation="vertical" android:gravity="right" android:background="#0A3F20" android:padding="10dip">

    <TextView
        android:id="@+id/txtHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="select font :"
        android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#ffffff" android:layout_marginBottom="5dip"/>

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right" >

        <RadioButton
            android:id="@+id/radioFontTitr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:checked="true"
            android:drawableRight="@android:drawable/btn_radio"
            android:tag="BTITRBD.TTF"
            android:text="font name" android:textColor="#ffffff"/>

        <RadioButton
            android:id="@+id/radioFontKoodak"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:drawableRight="@android:drawable/btn_radio"
            android:tag="b_koodak.ttf"
            android:text="font name 1" android:textColor="#ffffff"/>


        <RadioButton
            android:id="@+id/radioFontNazanin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:drawableRight="@android:drawable/btn_radio"
            android:tag="BNaznnBd.ttf"
            android:text="font name 2"
            android:textColor="#ffffff" />

    </RadioGroup>
</LinearLayout>

0 个答案:

没有答案