본문 바로가기

안드로이드 개발/개발팁

해상도 무관하게 화면 비율 고정

    @Override
    protected void attachBaseContext(Context newBase) {
        // 시스템 "화면 크게/작게" 설정을 무시하고 고정 density 적용
        android.content.res.Configuration config = new android.content.res.Configuration(newBase.getResources().getConfiguration());
        android.util.DisplayMetrics metrics = newBase.getResources().getDisplayMetrics();

        // 실제 픽셀 너비 기준으로 항상 360dp 화면이 되도록 density 고정
        float targetWidthDp = 360f;
        config.densityDpi = (int) (metrics.widthPixels / targetWidthDp * 160f);
        config.fontScale = 1.0f; // 글꼴 크기 설정도 무시

        super.attachBaseContext(newBase.createConfigurationContext(config));
    }