1)修改字体大小的方法涉及到一个ActivityManagerNative的类,而这个类是hide,类型。因此需要在源码环境中编译,或者使用反射,我使用java的反射来实现调用。
2)成功编译出apk后,安装测试,也没有反应,最后发现,如果需要修改configuration中的一些参数的话,需要在manifest.xml中加入这么一句 android:configChanges="fontScale" (因为我是修改字体大小,因此后面的值为fontScale)
其中主要的方法很简单:
private Configuration mconfig = new Configuration();
private Spinner.OnItemSelectedListener mFontSizeChanged = new Spinner.OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> arg0, View v, int position, long id) { if(position == 0){ mconfig.fontScale = 0.75f;//设置要改变字体大小的值,默认为1.0f }else if(position == 1){ mconfig.fontScale = 1.0f; }else { mconfig.fontScale = 1.25f; } } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } };
public void setFontSize(){ Method method; try { Class<?> activityManagerNative = Class.forName("android.app.ActivityManagerNative"); try { Object am = activityManagerNative.getMethod("getDefault").invoke(activityManagerNative); method = am.getClass().getMethod("updateConfiguration", android.content.res.Configuration.class); method.invoke(am,mconfig);//设置字体大小的方法就是updateConfiguration(Configuration confit); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
完整代码:
package com.cn.asus.test; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import android.app.Activity; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; public class TestMusicActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ private Configuration mconfig = new Configuration(); private DisplayMetrics mDisplayMetrics; private Spinner mFontSize; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mFontSize = (Spinner)findViewById(R.id.fontSize); mFontSize.setOnItemSelectedListener(mFontSizeChanged); String [] status = new String[3]; Resources r = getResources(); status[0] = r.getString(R.string.small_font); status[1] = r.getString(R.string.medium_font); status[2] = r.getString(R.string.large_font); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,status); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); Button save = (Button)findViewById(R.id.save); save.setText(r.getText(R.string.save)); save.setOnClickListener(this); DisplayMetrics metrics = getResources().getDisplayMetrics(); mDisplayMetrics = new DisplayMetrics(); mDisplayMetrics.density = metrics.density; mDisplayMetrics.heightPixels = metrics.heightPixels; mDisplayMetrics.scaledDensity = metrics.scaledDensity; mDisplayMetrics.widthPixels = metrics.widthPixels; mDisplayMetrics.xdpi = metrics.xdpi; mDisplayMetrics.ydpi = metrics.ydpi; } @Override public void onResume(){ super.onResume(); try { Class<?> activityManagerNative = Class.forName("android.app.ActivityManagerNative"); try { Object am = activityManagerNative.getMethod("getDefault").invoke(activityManagerNative); Object config = am.getClass().getMethod("getConfiguration").invoke(am); Configuration configs = (Configuration)config; mconfig.updateFrom(configs); if(mconfig.fontScale < 1){ mFontSize.setSelection(0); }else if(mconfig.fontScale > 1 ){ mFontSize.setSelection(2); }else { mFontSize.setSelection(1); } } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onClick(View v){ setFontSize(); finish(); } public void setFontSize(){ Method method; try { Class<?> activityManagerNative = Class.forName("android.app.ActivityManagerNative"); try { Object am = activityManagerNative.getMethod("getDefault").invoke(activityManagerNative); method = am.getClass().getMethod("updateConfiguration", android.content.res.Configuration.class); method.invoke(am,mconfig); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private Spinner.OnItemSelectedListener mFontSizeChanged = new Spinner.OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> arg0, View v, int position, long id) { if(position == 0){ mconfig.fontScale = 0.75f; }else if(position == 1){ mconfig.fontScale = 1.0f; }else { mconfig.fontScale = 1.25f; } } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }; }