我遇到的问题表明,为给定活动的layoutXML 选择的资源桶与从values文件夹中选择的资源不一致,尽管在每组文件夹中使用完全相同的资源限定符.
在我的应用程序的抽象父活动中放置一些日志代码后,我可以看到,当通过Nexus 7类型模拟器(Android 4.1)启动我的应用程序时,最小宽度确实是600dp,该layout-sw600dp-*文件夹用于获取活动的UI但是用于valuesis的文件夹values-large-*.我希望这能values-sw600dp-*为我提供关于活动所运行的资源桶的重要信息.
代码在我的应用程序的父活动中记录所有android.app.Activitys
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Configuration config = getResources().getConfiguration();
Log.i(this.getClass().getSimpleName(), String.format("Smallest width is [%s]", config.smallestScreenWidthDp));
configurationContext = SupportedDeviceConfiguration.fromResourceQualifer(getString(string.resourceQualifier));
Log.i(this.getClass().getSimpleName(), String.format("Running under the [%s] configuration context.", configurationContext.getResourceQualifier()));
...
Run Code Online (Sandbox Code Playgroud)
记录在Nexus 7类型设备上运行此代码时的输出;
[Logging fluff] Smallest width is [600]
[Logging fluff] Running under the [layout-large-land] configuration context.
Run Code Online (Sandbox Code Playgroud)
我知道你在想什么 - 那个布局大片土地的衍生来自哪里?继续阅读......
我正在试验这里概述的方法,这将允许我在运行时检查正在使用的资源桶.基本上我实现的方法具有以下资源限定符结构;
- res …Run Code Online (Sandbox Code Playgroud) android android-layout android-screen-support android-resources
我为不同的屏幕尺寸和设备使用不同的布局.我将片段与特定的布局文件夹一起使用.这个概念很棒,对于平板电脑和大屏幕设备,我将布局文件放在 layout-sw600dp中,Android设法在不同的设备上提供正确的布局.
我的错误是:如何找到我的代码中使用的布局.我的片段需要稍微不同的代码用于不同的布局.
总的来说,在我的片段/活动中分离自定义布局编程逻辑的最佳实践是什么?
我的方法现在有点hacky并且与不同的Layout文件夹不同步.
private boolean isTabletDevice() {
if (android.os.Build.VERSION.SDK_INT >= 11) { // honeycomb
// test screen size, use reflection because isLayoutSizeAtLeast is
// only available since 11
Configuration con = getResources().getConfiguration();
try {
Method mIsLayoutSizeAtLeast = con.getClass().getMethod("isLayoutSizeAtLeast", int.class);
Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE
return r;
} catch (Exception x) {
x.printStackTrace();
return false;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
然后
if(isTabletDevice()) {
//findViewById(R.id.onlyInTabletLayoutButton);
}else{
//
}
Run Code Online (Sandbox Code Playgroud)