Disabling an app or activity zoom if Setting -> Display -> Display size changed to Large or small

假装没事ソ 提交于 2019-12-30 11:20:08

问题


In my app, I don't want to allow it to resize as its creating design issues.

I have tried with android:resizeableActivity="false" in the application tag and the launcher activity tag but it didn't help.


回答1:


I have found a solution for it.

If system text size changed or Display size set to Large (Above Android Oreo), Your app will behave as normal (No big text and and zoomed views) with below code:

        Configuration configuration = getResources().getConfiguration();
        configuration.fontScale = (float) 1; //0.85 small size, 1 normal size, 1,15 big etc
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        metrics.scaledDensity = configuration.fontScale * metrics.density;
        configuration.densityDpi = (int) getResources().getDisplayMetrics().xdpi;
        getBaseContext().getResources().updateConfiguration(configuration, metrics);



回答2:


I will assume you're trying to get display which doesn't change if user changes Accessibility Options on it's phone regarding UI size.

You can get this working by using DisplayMetrics.

DisplayMetrics metrics = getResources().getDisplayMetrics();

With "normal" scale factor values metrics.xdpi and metrics.densityDpi have the same value. If this is not the case, you can get "real" scale factor, that is one used when user uses normal scaling using following.

if(metrics.xdpi != metrics.densityDpi){
        Log.d(TAG,"Real scale " +  (metrics.xdpi / metrics.densityDpi)*metrics.density);
}

Than you can use said value to multiply your fixed values from layout so that it can have precise density on different screens. This would also mean that every value in layout must be using px instead of dp or sp.


While this approach would work, I would strongly recommend not to use it.

Firstly, because it is a lot of work, and it will be hard to maintain that code with future updates.

Secondly, this feature of Android is very convenient, and if your app doesn't compile with it it probably points out that you should construct your layout better.



来源:https://stackoverflow.com/questions/50252424/disabling-an-app-or-activity-zoom-if-setting-display-display-size-changed

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