Android的Java中更改densityDPIAndroid、Java、densityDPI

2023-09-07 10:25:36 作者:生活就是屁颠屁颠的过

剂量任何一个知道如何更改densityDpi在Java的Andr​​oid应用?

Dose any one know how to change the densityDpi for an android app in java?

我工作的一个Android应用程序,我操纵的观点标签的目标密度。这在一些地方的应用程式的导致一个小问题,目标密度似乎是由软键盘进行复位。

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.

所以,我需要一种方法来更改密度复位此,或prevent键盘。

So I need a way to reset this, or prevent the keyboard from changing the density.

推荐答案

对于像联想A1(片剂具有240dpi密度)设备或星系注意,您可能需要为此在你的应用程序,以避免一切在变化看起来就像它的跨设备的尺寸。

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.

添加以下功能到您的扩展应用类,并从的onCreate调用它:

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);
}