Android的资源选择布图设计和值时─不一致资源、Android

2023-09-05 06:10:15 作者:自衬

我遇到的问题表明,被选择进行特定活动的布局资源桶 XML是从值被选中的资源不一致尽管完全相同的资源预选赛文件夹被用在每一组的文件夹。

The issue I am experiencing indicates that the resource bucket being selected for a given activity's layout XML is inconsistent with the resources being selected from the values folder despite the exact same resource qualifiers being used in each set of folders.

把一些日志记录code在我的应用程序的抽象父活动后,我可以看到,开始我的应用程序在一台Nexus 7型仿真器(安卓4.1)时的最小宽度确实600dp,在布局-sw600dp - * 文件夹被用来获取用户界面的活动,但正在使用的值的文件夹价值观大 - * 。我原以为这是价值观sw600dp - * 从而为我提供了重要的信息,该活动在其下运行什么资源桶

After placing some logging code within my application's abstract parent activity I can see that when starting my application over a Nexus 7 type emulator (Android 4.1) the smallest width is indeed 600dp, the layout-sw600dp-* folder is being used to fetch the UI for the activity but the folder being used for the values is values-large-*. I was expecting this to be values-sw600dp-* thus providing me with the vital information as to what resource bucket the activity is running under.

code做着记录在我的应用程序的父活动的所有 android.app.Activity 取值

Code doing the logging within my app's parent activity for all 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()));
...

记录从我一台Nexus 7类型的设备上运行该code输出;

Logging output from when I run this code on a Nexus 7 type device;

[Logging fluff] Smallest width is [600]
[Logging fluff] Running under the [layout-large-land] configuration context.

我知道你在想什么 - 在哪里说的布局大的土地的推导从何而来?阅读...

I know what you are thinking - where did that layout-large-land derivation come from? Read on...

我试用的做法概括这里这将让我在使用检查资源桶运行。基本上我已经实现了方法具有资源预选赛的结构如下;

I am trialling an approach outlined here which would allow me to inspect the resources bucket under use at runtime. Essentially the approach I have implemented has the following structure of resource qualifiers;

- 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

所以本质上是预选赛反映了布局预选赛。在每个值 - * 文件夹我已经定义了一个名为设备-config.xml中与内容的XML文件;

So essentially the values qualifiers reflect that of the layout qualifiers. Under each of values-* folders I have defined a single XML file called device-config.xml with content;

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="resourceQualifier">layout-{qualifier of values folder}</string>
</resources>

所以,例如价值观sw600dp陆文件夹的设备-config.xml中包含了一个字符串值布局sw600dp陆。这里的目标是为我的code与显示屏幕上的资源布局保持同步。这是必要的,这样我的code不熄灭发现通过ID不上由于房地产涉及的显示布局存在一定的项目。

So, for example the values-sw600dp-land folder's device-config.xml contains a single string with value layout-sw600dp-land. The objective here is for my code to remain in-sync with the resource layouts being displayed on screen. This is needed so that my code doesn't go off "finding by id" some item which doesn't exist on the displayed layout owing to the real-estate involved.

对于想知道桶的更深层次的理由被用来在运行时诞生出实现我的一个片段换全配置code变得难以管理与各种基于交换机的逻辑,是不是从其他布局透明和经常重复的特点,如果我需要某种片段继承 ...这是......这,如果你按照链接正是我所做的。这种方法的缺点是,我需要知道什么屏幕我与指导框架之前,实例化X,Y或Z片段,安全的,该片段正在创建将永远不同步的布局知识的工作就是指膨胀。这种继承方式,并允许一个更易于管理的片段栈(声纳是更加快乐了,这是很好)。

The deeper reasoning for wanting to know the bucket being used at runtime was born out of the realisation that my single-fragment-for-all-configurations code was becoming difficult to manage with various switch based logic which was not transparent and often duplicated features from other layouts...it was as if I needed some sort of Fragment Inheritance ...which if you follow the link is exactly what I did. The downside of this is that I need to know what screen I am working with before instructing the framework to instantiate the x, y, or z fragment, safe in the knowledge that the Fragment being created will never be out of sync with the layout it is meant to inflate. This inheritance works and allows for a far more manageable fragment stack (Sonar is happier too which is nice).

不过,我已经挫败了这种明显的差异之间的布局文件夹,文件夹值的框架选择。每个人都有相同的预选赛,因此为什么不一个活动借力布局sw600dp陆 UI XML使用价值观sw600dp陆资源?我希望我有什么问题,因为它是贴在SO讨论中,我联系到上述可能的解决方案中最巧妙的。

However, I have been thwarted by this apparent discrepancy between which layout folder and values folder the framework selects. Each have the same qualifiers therefore why doesn't an Activity leveraging the layout-sw600dp-land UI XML use the values-sw600dp-land resource? I am hoping I've got something wrong because it was the neatest of the potential solutions posted on the SO discussion I linked to above.

推荐答案

我敢肯定,你处理的是用于选择资源precedence。

I'm sure you're dealing with resource precedence used for selection.

如果您提供的文件夹:

layout-sw600dp-*
values-large-*
values-sw600dp-*

安卓没有义务相匹配的值的选择文件夹复制到那些的布局的,而它使用同样的precedence逻辑分开布局,并分别对值的文件夹。

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.

您可以了解这里这个选择算法: http://developer.android.com/guide/topics/resources/providing-resources.html#BestMatch

You can learn about this selection algorithm here: http://developer.android.com/guide/topics/resources/providing-resources.html#BestMatch