Android Changing densityDPI in java

二次信任 提交于 2019-12-08 13:02:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!