改变字体大小

public class MyApp extends Application{
    public static int fontInt = 0;
    public  static Context context;
    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        //初始化
        x.Ext.init(this);
        //提高性能
        x.Ext.setDebug(false);
      
    }
    /**
     * 得到上下文
     * @return
     */
    public  static  Context getContext(){
        return context;
    }
}
//点击进行选择,弹出对话框
//显示改变字体对话框
private void showDialg() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    View view = View.inflate(this, R.layout.setting_dialog, null);
    CheckBox setting_dialog_small = (CheckBox) view.findViewById(R.id.setting_dialog_small);
    CheckBox setting_dialog_middle = (CheckBox) view.findViewById(R.id.setting_dialog_middle);
    CheckBox setting_dialog_big = (CheckBox) view.findViewById(R.id.setting_dialog_big);
    TextView setting_dialog_quTv = (TextView) view.findViewById(R.id.setting_dialog_quTv);
    builder.setView(view);
    final AlertDialog dialog = builder.show();
    setting_dialog_quTv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
        }
    });
    setting_dialog_small.setChecked(true);
    if (MyApp.fontInt == 1) {
        setting_dialog_small.setChecked(true);
        setting_dialog_middle.setChecked(false);
        setting_dialog_big.setChecked(false);
    } else if (MyApp.fontInt == 2) {
        setting_dialog_middle.setChecked(true);
        setting_dialog_small.setChecked(false);
        setting_dialog_big.setChecked(false);
    } else if (MyApp.fontInt == 3) {
        setting_dialog_big.setChecked(true);
        setting_dialog_small.setChecked(false);
        setting_dialog_middle.setChecked(false);
    }
    //小字体
    setting_dialog_small.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b) {
                MyApp.fontInt = 1;
                Toast.makeText(SheZhi_demo.this, "改变完成", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        }
    });
    //中号
    setting_dialog_middle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b) {
                MyApp.fontInt = 2;
                Toast.makeText(SheZhi_demo.this, "改变完成", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        }
    });
    //大号
    setting_dialog_big.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b) {
                MyApp.fontInt = 3;
                Toast.makeText(SheZhi_demo.this, "改变完成", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        }
    });
}
//写一个根据手机的分辨率dp的单位转换为px方法,进行适配调用
public class Ziti_demo {
    /**
     * 根据手机的分辨率dp的单位转为px(像素)
     * @param px
     * @return
     */
   public static int px2dip(int px) {
        //获取像素密度
        float density = MyApp.getContext().getResources().getDisplayMetrics().density;
        int dip = (int) (px / density + 0.5f);
        return dip;
    }
}
//进行数据适配。判断选中的数字进行修改字体大小
if(MyApp.fontInt == 1){
    holder1.text_item1_name.setTextSize(Ziti_demo.px2dip(15));
}else if(MyApp.fontInt == 2){
    holder1.text_item1_name.setTextSize(Ziti_demo.px2dip(20));
}else if(MyApp.fontInt == 3){
    holder1.text_item1_name.setTextSize(Ziti_demo.px2dip(30));
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值