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)); }
改变字体大小
最新推荐文章于 2023-03-30 15:42:28 发布