应用字体大小的设置

    xiaoxiao2021-03-25  174

    1.在application中进行注册 public class MyApplication extends Application { public static int info =1;//默认的字体的状态 public static Context context ; public static Context getContext(){ return context ; } 2.设置字体单位转变的的方法 public class WenUtils { /** * 根据手机的分辨率 dp 的单位转为 px( 像素 ) * @param px * @return */ public static int px2dip(int px) { // 获取像素密度 float density = MyApplication. getContext ().getResources().getDisplayMetrics().density;、 int dip = (int) (px / density + 0.5f); return dip; } } 3.设置点击事件,通过单选框进行给字的状态赋值 sliding__radio_sz.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 设置字体的大小 String str[] =new String[]{" "," "," "}; AlertDialog.Builder builder = new AlertDialog.Builder(NewsActivity.this); builder.setTitle(" 请选择设置的字体的大小 "); builder.setIcon(R.mipmap. icon ); builder.setSingleChoiceItems(str, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which){ case 0: //记录状态 MyApplication. info =0; dialog.dismiss(); break; case 1: MyApplication. info =1; dialog.dismiss(); break; case 2: MyApplication. info =2; dialog.dismiss(); break; } } }); builder.create().show(); } }); 4.在适配器中进行判断状态(info)是几,给字体赋大小 if(MyApplication. info ==0){ //调用设置字体大小的方法 holder1.title1.setTextSize(WenUtils. px2dip (15)); }else if(MyApplication. info ==1){ holder1.title1.setTextSize(WenUtils. px2dip (20)); }else{ holder1.title1.setTextSize(WenUtils. px2dip (30)); }
    转载请注明原文地址: https://ju.6miu.com/read-22271.html

    最新回复(0)