【问题标题】:Is there something similar to Android's dimens.xml in iOS?iOS中是否有类似于Android的dimens.xml的东西?
【发布时间】:2017-11-16 01:07:22
【问题描述】:

我最近发现我应该将我所有的尺寸/尺寸放在dimens.xml 中,以便支持多种屏幕分辨率的 Android 应用程序。

我应该在 iOS 中做一些类似的事情来支持多种屏幕分辨率吗?我在后端对所有视图进行编码(没有故事板),当我想定位视图(填充、边距等)时,我只需将硬编码的数字添加到视图 @ 的 X 和 Y 属性中987654324@。 Width、Height、Font size 等也是如此。我想我这样编写的 iOS 应用程序在不同的屏幕分辨率下看起来会很尴尬?

顺便说一句,我正在使用 Xamarin(Xamarin.Android 和 Xamarin.iOS)。但我会使用本机代码理解答案。

【问题讨论】:

    标签: android ios xamarin


    【解决方案1】:

    在 iOS 中没有像 dimens 这样的东西。

    我相信AutoLayout 的适当约束可以在padding、margins、Width、Height 等上运行良好。

    但是,如果您仍然想找到一种方法来实现它,这里有一个临时解决方法。

    创建一个名为Dimens的静态类。

    public static class Dimens
    {
        public static int Height
        {
            get {
                string device = DeviceHardware.Model;
    
                if (device.Contains("iPhone4"))
                {
                    return 10;
                }
                else if (device.Contains("iPhone5"))
                {
                    return 15;
                }
                else if (device.Equals("iPhone 6") || device.Equals("iPhone 6S"))
                {
                    return 20;
                }
                else if (device.Equals("iPhone 6 Plus") || device.Equals("iPhone 6S Plus"))
                {
                    return 25;
                }
                //other device 
    
                return 0;
            }
        }      
    }
    

    用法:

    View.Height = Dimens.Height;
    

    Here 是可以检查您的设备类型的插件。

    【讨论】:

    • 1+ 和我的想法一样。
    【解决方案2】:

    您可以创建一个空的 swift 文件,并在 Android 上使用 struct 而不是 dimens.xml

    struct Padding {
        static let left = 10
        static let right = 10
        static let top = 10
        static let bottom = 10
    }
    

    【讨论】:

    • 那么我如何选择哪个struct 或 swift 文件用于运行我的应用程序的设备?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 2011-12-04
    • 2011-01-07
    • 1970-01-01
    • 2021-09-30
    相关资源
    最近更新 更多