如何访问 Android 资源,例如libgdx 中的维度?

How to access Android resources e.g. dimensions in libgdx?

我需要从 libgdx.

的核心目录 类 的 Android 文件夹下的 XML 文件中获取定义的维度

我知道我可以使用资产文件夹来访问可绘制对象。但是维度或字符串呢?

您无法直接在核心项目中访问此类特定于平台的信息。但这可以通过将这些功能与 Facade 接口来实现,为每个目标提供 platform-specific 实现。

In libgdx-wiki 有平台特定接口的示例。

public interface PlatformSpecificStuff {
    public String getString();
    public int getDimensions();
}

// In -Android project
public class AndroidSpecificStuff implements PlatformSpecificStuff {
    public String getString(){
        // do whaterver you want with the resources.
        return "string resource";
    }

    public int getDimensions(){
        // do whaterver you want with the resources.
        return 42;
    }
}

// In -Desktop project
public class DesktopSpecificStuff implements PlatformSpecificStuff {
    public String getString(){
        // there is no xml or any andorid specific resource
        return null;
    }

    public int getDimensions(){
        // there is no xml or any andorid specific resource
        return 0;
    }
}

public class MyGame implements ApplicationListener {
    private final PlatformSpecificStuff pss;

    public MyGame(PlatformSpecificStuff pss) {
        this.pss= pss;
    }
}

更新:我不知道通过获取维度作为 android 资源试图完成什么。但你可能走错了路。如果您想支持多种屏幕尺寸,请查看 viewports