Oce*_*ife 38 android android-layout android-screen-support android-resources
我遇到的问题表明,为给定活动的layout
XML 选择的资源桶与从values
文件夹中选择的资源不一致,尽管在每组文件夹中使用完全相同的资源限定符.
在我的应用程序的抽象父活动中放置一些日志代码后,我可以看到,当通过Nexus 7类型模拟器(Android 4.1)启动我的应用程序时,最小宽度确实是600dp,该layout-sw600dp-*
文件夹用于获取活动的UI但是用于values
is的文件夹values-large-*
.我希望这能values-sw600dp-*
为我提供关于活动所运行的资源桶的重要信息.
代码在我的应用程序的父活动中记录所有android.app.Activity
s
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
+ layout // Default portrait layout.
+ layout-land // Default landscape layout
+ layout-large-land // pre 3.2 phablet landscape layout (Galaxy Note at v2.3.3)
+ layout-xlarge-land // pre 3.2 tablet landscape layout
+ layout-xlarge-port // pre 3.2 tablet portrait layout
+ layout-sw520dp-port // post 3.1 phablet portrait layout (Galaxy Note at v4.0.3)
+ layout-sw520dp-land // post 3.1 phablet landscape layout
+ layout-sw600dp-port // post 3.1 mini-tablet portrait layout (Nexus 7)
+ layout-sw600dp-land // post 3.1 mini-tablet-landscape layout
+ layout-sw700dp-port // post 3.1 tablet portrait layout
+ layout-sw700dp-land // post 3.1 tablet landscape layout
- values // Contains the root strings.xml
strings.xml
- values-land
default-config.xml
- values-large-land
default-config.xml
- values-xlarge-land
default-config.xml
- values-xlarge-port
default-config.xml
- values-sw520dp-port
default-config.xml
- values-sw520dp-land
default-config.xml
- values-sw600dp-port
default-config.xml
- values-sw600dp-land
default-config.xml
- values-sw700dp-port
default-config.xml
- values-sw700dp-land
default-config.xml
Run Code Online (Sandbox Code Playgroud)
因此,基本上values
限定符反映了layout
限定符的限定符.在每个values-*
文件夹下,我定义了一个device-config.xml
用内容调用的XML文件;
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="resourceQualifier">layout-{qualifier of values folder}</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
因此,例如values-sw600dp-land
文件夹device-config.xml
包含一个带有值的字符串layout-sw600dp-land
.这里的目标是让我的代码与屏幕上显示的资源布局保持同步.这是必要的,以便我的代码不会"通过id"找到由于所涉及的房地产而在显示的布局上不存在的某个项目.
想要了解在运行时使用的存储桶的更深层次的理由源于我认为我的单片段所有配置代码变得难以通过各种基于交换机的逻辑来管理,这些逻辑不透明并且通常是来自其他布局......就好像我需要某种片段继承 ......如果你按照链接这就是我所做的.这样做的缺点是,在指示框架实例化x,y或z片段之前,我需要知道我正在使用的屏幕,安全地知道正在创建的片段永远不会与布局不同步意在膨胀.这种继承有效,并且允许更易于管理的片段堆栈(Sonar也更快乐,这很好).
但是,我被框架选择的布局文件夹和值文件夹之间明显的差异所阻挠.每个都有相同的限定符,因此为什么不利用layout-sw600dp-land
UI XML 的Activity 使用values-sw600dp-land
资源呢?我希望我有一些错误,因为这是我在上面讨论的SO讨论中发布的最有可能的解决方案.
Poi*_*ull 17
I'm sure you're dealing with resource precedence used for selection.
If you provide folders:
layout-sw600dp-*
values-large-*
values-sw600dp-*
Run Code Online (Sandbox Code Playgroud)
Android is not obliged to match values selection folder to those of layout, rather it uses same precedence logic separately for layout and separately for values folder.
You can learn about this selection algorithm here: http://developer.android.com/guide/topics/resources/providing-resources.html#BestMatch