问题
Dose any one know how to change the densityDpi for an android app in java?
I'm working on an android application where i manipulate the target density in the viewpoint tag. This is causing a minor problem in some place of the apps, the target density seems to be reset by the soft keyboard.
So I need a way to reset this, or prevent the keyboard from changing the density.
回答1:
For devices like the lenovo A1 (tablet with 240dpi density) or the galaxy note you may need to do this in your app to avoid everything looking like its at varying sizes across devices.
Add the following function to your extended application class and call it from the onCreate:
public void changeDensity(float desiredDensity) {
//desiredDensity : ldpi = 0.75 (120dpi) , mdpi = 1 (160dpi), hdpi = 1.5 (240dpi), xhdpi = 2.0 (320dpi)
DisplayMetrics metrics = getResources().getDisplayMetrics();
metrics.density = desiredDensity;
metrics.xdpi = desiredDensity * 160;
metrics.ydpi = desiredDensity * 160;
metrics.densityDpi = (int) (desiredDensity * 160);
getResources().updateConfiguration(null, null);
}
来源:https://stackoverflow.com/questions/11874997/android-changing-densitydpi-in-java