设备显示设置(即 Android 屏幕缩放和字体) [英] Devices Display Settings (ie Android Screen Zoom & Font)

查看:65
本文介绍了设备显示设置(即 Android 屏幕缩放和字体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是在我们现有的 IOS 应用程序旁边完成一个 Andoid 应用程序.Android 的项目框架已经存在,我只需要填补空白以使其跟上 IOS 版本的速度.

I have been tasked to complete an Andoid app alongside our existing IOS app. The project skeleton for Android is already there I just need to fill in the gaps to bring it up to speed with the IOS version.

我一直在通过自己的手机进行测试,并且一直在为 Galaxy S6 上的字体过大而苦苦挣扎.现在我刚刚意识到这是由于我设备上的实际缩放和字体显示设置(我的眼睛不是最好的).

I have been testing via my own phone and have been struggling with fonts being too large on my Galaxy S6. Now I have just realised that this is due to the actual Zomm and Font display settings on my device (my eyesite isn't the best).

我的问题是,是否可以从我的设备中检索这些缩放设置,以便我可以相应地调整字体等?

My question is, is it possible to retrieve these zoom settings from my device so that I can adjust font's etc accordingly?

对 Xamarin 来说还很陌生,但我已经到了那里 - 希望你能提供一些指导来帮助我走得更远.

Pretty new to Xamarin but I'm getting there - hopefully you'll have some pointers to help me get further.

推荐答案

众所周知,dp和px的转换公式:px = dp * density

As we all know, the conversion formula from dp and px: px = dp * density

可以看出,如果设计宽度为360dp,如果要保证所有设备计算出来的px值正好是屏幕的宽度,我们只能修改密度的值.

It can be seen that if the design width is 360dp, we can only modify the value of density if we want to ensure that the px values calculated by all devices are exactly the width of the screen.

通过阅读源代码和官方文档,我们可以看到密度是 DisplayMetrics 中的成员变量,DisplayMetrics 实例可通过 Resources#getDisplayMetricsResouces 获得> 通过 Activity 或 Application Context 获取.

By reading the source code and official document, we can see that the density is a member variable in DisplayMetrics, and the DisplayMetrics instance is available through Resources#getDisplayMetrics, and the Resouces is obtained through the Activity or Application Context.

我们可以熟悉以下与 DisplayMetrics 中和适应相关的变量:

We can familiarize ourselves with the following variables related to DisplayMetrics neutralization adaptation:

  • DisplayMetrics#density 是上面的密度
  • DisplayMetrics#densityDpi 是上面的 dpi
  • DisplayMetrics#scaledDensity 字体的缩放因子,即等于正常条件下的密度,但会改变这个调整系统字体大小后的值.
  • DisplayMetrics#density is the above density
  • DisplayMetrics#densityDpi is the above dpi
  • DisplayMetrics#scaledDensity The scaling factor of the font, which is equal to the density under normal conditions, but will change this value after adjusting the system font size.

解决方案:

以下假设设计图宽度为360dp,适配宽尺寸.

The following assumes that the design map width is 360dp and is adapted to the wide dimension.

那么适配后的密度=设备实际宽度(单位px)/360,那么我们只需要在系统中修改我们计算出来的密度即可,代码实现如下:

Then the adapted density = device real width (unit px) / 360, then we only need to modify our calculated density in the system, the code is implemented as follows:

private static void setCustomDensity( Activity activity,Application application)
 {
    DisplayMetrics appDisplaymetrics = application.Resources.DisplayMetrics;
    float targetDensity = appDisplaymetrics.WidthPixels / 360;
    int targetDensityDpi = (int)(160 * targetDensity);

    appDisplaymetrics.Density = appDisplaymetrics.ScaledDensity = targetDensity;
    appDisplaymetrics.DensityDpi = (Android.Util.DisplayMetricsDensity)targetDensityDpi;

    DisplayMetrics activityDisplayMetrics = activity.Resources.DisplayMetrics;
    activityDisplayMetrics.Density = activityDisplayMetrics.ScaledDensity = targetDensity;
    activityDisplayMetrics.DensityDpi = (Android.Util.DisplayMetricsDensity)targetDensityDpi;
}

也在 Activity#onCreate 方法中调用.代码比较简单,不涉及系统非公开api的调用,理论上不会影响app的稳定性.

Also called in the Activity#onCreate method. The code is relatively simple, and does not involve the call of the system non-public api, so theoretically it will not affect the stability of the app.

注意:如果您在系统设置中切换字体,然后返回应用程序,字体并没有改变.所以必须监听字体切换,调用Application#registerComponentCallbacks注册onConfigurationChanged监听器.

Note: If you switch fonts in the system settings and then return to the app, the fonts have not changed. So you have to listen to the font switch, call Application#registerComponentCallbacks to register the onConfigurationChanged listener.

这篇关于设备显示设置(即 Android 屏幕缩放和字体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆