如何在android中通过编程和用户选择设置文本大小

如何在android中通过编程和用户选择设置文本大小,android,Android,我想更改textView的文本大小属性,或用户在我的选项对话框中更改我应用程序中的任何文本基。但我不知道用什么方法来做这件事。该工作是否有标准?请查看以下内容: TextView tv=new TextView(this); //either: tv.setTextAppearance(this, R.style.textStyle1); //some style you have set up ///or: tv.setTextSize(User

我想更改
textView
的文本大小属性,或用户在我的选项对话框中更改我应用程序中的任何文本基。但我不知道用什么方法来做这件事。该工作是否有标准?

请查看以下内容:

    TextView tv=new TextView(this);

    //either:
    tv.setTextAppearance(this, R.style.textStyle1); //some style you have set up


    ///or: 
    tv.setTextSize(UserChoice);
    tv.setTextColor(R.color.myred); //some color you set up

更改我的应用程序中文本大小的我的类:

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class AppStyle {

    private List<ViewGroup> _Groups;
    private static float _TextSize;
    private static SharedPreferences _Prefs;

    public AppStyle(Context context, ViewGroup[] groups) {

        _Groups = new ArrayList<ViewGroup>();

        for (ViewGroup viewGroup : groups) {
         _Groups.add(viewGroup);
        }

        _Prefs = PreferenceManager.getDefaultSharedPreferences(context);
        setTextSize(_Prefs.getFloat("AppTextSize", 15));

        findViewInGroups();
    }

    private void findViewInGroups() {

        List<ViewGroup> Groups = new ArrayList<ViewGroup>();
        for (ViewGroup viewGroup : _Groups) {
            Groups.add(viewGroup);
        }

        _Groups.clear();
        for (ViewGroup viewGroup : Groups) {

            for (int i = 0; i < viewGroup.getChildCount(); i++) {

                if (viewGroup.getChildAt(i) instanceof ViewGroup) {
                    _Groups.add((ViewGroup) viewGroup.getChildAt(i));

                } else if (viewGroup.getChildAt(i).getId() != -1) {   //For special occasions

                    if (viewGroup.getChildAt(i) instanceof TextView) { //for TextView

                        ((TextView) viewGroup.getChildAt(i)).setTextSize(
                                TypedValue.COMPLEX_UNIT_SP, _TextSize);

                    } else if (viewGroup.getChildAt(i) instanceof EditText) { //for EditText

                        ((EditText) viewGroup.getChildAt(i)).setTextSize(
                                TypedValue.COMPLEX_UNIT_SP, _TextSize);

                    } else if (viewGroup.getChildAt(i) instanceof Button) {//for Button

                        ((Button) viewGroup.getChildAt(i)).setTextSize(
                                TypedValue.COMPLEX_UNIT_SP, _TextSize);

                    } else if (viewGroup.getChildAt(i) instanceof CheckBox) {//for CheckBox

                        ((CheckBox) viewGroup.getChildAt(i)).setTextSize(
                                TypedValue.COMPLEX_UNIT_SP, _TextSize);
                    }
                }

            }

        }
        if (_Groups.size() != 0) {

            findViewInGroups();
        } else {
            _Groups = null;
            return;
        }

    }

    public static void setTextSize(float textSize) {
        _TextSize = textSize;
        SharedPreferences.Editor editor = _Prefs.edit();
        editor.putFloat("AppTextSize", textSize);
        editor.commit();
    }

}
import java.util.ArrayList;
导入java.util.List;
导入android.content.Context;
导入android.content.SharedReferences;
导入android.preference.PreferenceManager;
导入android.util.TypedValue;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.CheckBox;
导入android.widget.EditText;
导入android.widget.TextView;
公共类AppStyle{
私有列表组;
私有静态浮动_TextSize;
私有静态共享引用;
公共AppStyle(上下文,视图组[]组){
_Groups=newarraylist();
用于(视图组视图组:组){
_添加(视图组);
}
_Prefs=PreferenceManager.GetDefaultSharedReferences(上下文);
setTextSize(_Prefs.getFloat(“AppTextSize”,15));
findViewGroups();
}
私有void findViewInGroups(){
列表组=新的ArrayList();
对于(视图组视图组:\组){
添加(视图组);
}
_组。清除();
用于(视图组视图组:组){
对于(int i=0;i

但我意识到这种方法给我的应用程序带来了负担。

没关系,但我需要立即更改所有项目,在我的应用程序中,文本样式的项目很多。这个方法对我不可用!考虑修改