如何区分16:9显示屏和18:9显示屏?

Sot*_*nas 2 android screen-orientation android-layout screen-size android-resources

在我的Android应用中,我使用不同的Android资源目录来支持多种屏幕尺寸和方向。所以在res我里面有一个layout文件夹,一个layout-land文件夹,一个layout-large文件夹,一个layout-large-land文件夹等等。

在我尝试将我的应用安装到具有18:9显示屏而不是显示屏的较新手机中之前,这种方法对我来说非常有效16:9

该电话从layoutlayout-land文件夹中提取资源,这意味着它将被注册为“常规”屏幕。

此外,如果我尝试使用以下代码获取此手机的屏幕尺寸,

int screenSize = getResources().getConfiguration().screenLayout &
                Configuration.SCREENLAYOUT_SIZE_MASK;


        switch(screenSize) {
            case Configuration.SCREENLAYOUT_SIZE_XLARGE:
                Toast.makeText(this, "Extra Large Screen", Toast.LENGTH_LONG).show();
                break;
            case Configuration.SCREENLAYOUT_SIZE_LARGE:
                Toast.makeText(this, "Large Screen", Toast.LENGTH_LONG).show();
                break;
            case Configuration.SCREENLAYOUT_SIZE_NORMAL:
                Toast.makeText(this, "Normal Screen", Toast.LENGTH_LONG).show();
                break;
            case Configuration.SCREENLAYOUT_SIZE_SMALL:
                Toast.makeText(this, "Small Screen", Toast.LENGTH_LONG).show();
                break;
            default:
                Toast.makeText(this, "Screen size is neither xlarge, large, normal or small", Toast.LENGTH_LONG).show();
        }
Run Code Online (Sandbox Code Playgroud)

然后再次将其注册为正常屏幕。但是具有16:9显示屏的手机也是如此。不用说,这在这两款手机上的应用外观上产生了微小但明显的差异。

所以我的问题是,有没有办法区分这两种显示?

注意:我知道有一些解决方法,例如dp完全避免使用值并仅使用权重,但是我想知道的是,是否存在“官方”或“建议”方法来处理这种情况。很像layout-land用于横向放置的文件夹或layout-xlarge用于超大屏幕的文件夹等等。

提前致谢。

She*_*yar 5

Device fragmentation has always been a huge problem for Android. Sadly, the same thing that makes Android so powerful is also a huge source of pain. You can't really predict how the device manufacturers will change different aspects of the device including the screen and the aspect ratio.

But you can check the ratio yourself and make changes according to that (but I guess this is not "official" way you had in mind):

DisplayMetrics metrics = context.getResources().getDisplayMetrics();
float ratio = ((float)metrics.heightPixels / (float)metrics.widthPixels);
Run Code Online (Sandbox Code Playgroud)

Source