You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Android设备上程序化修改屏幕DPI并实现重启持久化?

Hey there, let's break down how to solve this problem—since you've already confirmed that adjusting the DPI to 1.5 fixes your app's display, the key is making that change stick after a reboot. Here are your options, ordered by practicality and ease of implementation:

Instead of altering the entire system's DPI (which affects all apps), you can tweak your app to render at the 1.5x density regardless of the device's default setting. This is the safest approach, requires no special permissions, and won't impact other apps on the tablet.

How to implement it:

  • In your app's Application class, override the onCreate() method to adjust the display metrics for your app alone:
    @Override
    public void onCreate() {
        super.onCreate();
        // Get the current display metrics
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        // Force 1.5x density (matches your target DPI)
        metrics.density = 1.5f;
        metrics.densityDpi = (int) (160 * 1.5); // 160 is the base mdpi density
        metrics.scaledDensity = metrics.density; // Ensure text scales correctly
        // Update the configuration
        getResources().updateConfiguration(getResources().getConfiguration(), metrics);
    }
    
  • Alternatively, you can create density-specific resource folders (like values-hdpi instead of relying on the device's auto-scaling) to ensure your layouts and assets render as intended.

2. Persistent System-Wide DPI Change with Root Access

If you need a system-wide fix (affecting all apps), you'll need root access to modify system files or persistent properties. This change will survive reboots.

Option A: Modify build.prop

  1. Use a root-enabled file explorer or adb shell (with root) to remount the /system partition as writable:
    adb shell su -c "mount -o remount,rw /system"
    
  2. Edit the /system/build.prop file, add or modify this line to set your target DPI (1.5x = 240 DPI, since base mdpi is 160):
    ro.sf.lcd_density=240
    
  3. Remount /system as read-only to protect it:
    adb shell su -c "mount -o remount,ro /system"
    
  4. Reboot the tablet—your DPI setting will persist.

Option B: Set a Persistent System Property

  1. Run this adb command (with root) to set a persistent density value:
    adb shell su -c "setprop persist.sys.density 240"
    
  2. Reboot the device. The persist. prefix ensures the setting is saved across reboots.

3. System App or Secure Settings Permission (No Root, Restricted Access)

If you can sign your app with the device's system signature (making it a system app) or grant it the WRITE_SECURE_SETTINGS permission, you can modify the system's DPI setting programmatically without root.

Steps for non-system apps (requires adb):

  1. Grant the permission to your app via adb:
    adb shell pm grant com.your.app.package android.permission.WRITE_SECURE_SETTINGS
    
  2. In your app code, use Settings.Global to set the desired DPI:
    import android.provider.Settings;
    import android.content.ContentResolver;
    
    // Inside your activity/service
    ContentResolver resolver = getContentResolver();
    Settings.Global.putInt(resolver, Settings.Global.DENSITY_DPI, 240);
    // Restart the system UI to apply changes immediately
    Runtime.getRuntime().exec("am restart");
    
    This change is stored in the system's settings database and will survive reboots. Note that some devices may block this for non-system apps, even with the permission.

内容的提问来源于stack exchange,提问作者Javito Kanekanoa

火山引擎 最新活动