3

I know that this behaviour is not expected nor standard, but I'm curous whether it can be achieved. Google also realized that the normal-large-xlarge scheme is not the best one, so it's deprecated from Android 3.2. (layout-swXXXdp would be the best choice for me)

However, there are devices under 3.2 which I have to support, and can misbehave because this system.

Main problem:

Galaxy Note 1 and Galaxy tab 1 (and other devices with same specs) choose the resources from the same layout-large folder. But reasonably I need to make different layouts under 7 inch and above 7inch screens. I know that I could make a seperate folder for "exception devices" (like layout-large-xhdpi-1280x800 as mentioned here), but then I would have to maintain several almost (or exact) same xml files in different folders just because these certain devices.

So the goal would be to maintain only one xml package (folder) for under-7inch-devices, and one for above-7inch devices. I know that there are tricks such as:

  1. naming the xml-s differently (main.xml and main-large.xml), and decide in code which to choose, but even devices can't decide about themselves from code, how large are them exactly (for example 7 inch tablets can give back from 6.8 to 7.8 inch scale). Also, I would like to use the more "androidy" way with resource folders.
  2. maintain the exception folders as i mentioned. layout, layout-large, layout-specifictags1, layout-specifictags2 and the list can widen. Also specifictags1, tags2 and normal has the exact same xml files, but needed for devices not to choose from the layout-large. The whole reason is to avoid this.
  3. (EDIT) An idea of using aliases came up by @Joe Malin. Unfortunatly I can't make an alias named main.xml in layout-large-xhdpi-1280x800 that refers to main.xml in layout, because it refers to self then (making infinite loop). So i would have to maintain a fakemain.xml in layout-large-xhdpi-1280x800 that refers to main.xml which only exists in layout. But then i also need a fakemain.xml in layout, because then i don't have to care about the layout from code and use only the fakemain.xml which is an alias. However this solution still could work, but is not working in practice, unfortunatly. If I have a a fakemain.xml in layout, layout-large and layout-large-xhdpi-1280x800 for galaxy note, then the alias points to the layout-large one, and I achieved nothing by using the alias.

Question:

layout-sw600dp concept would grant me the best and needed behaviour, but it's not supported under API level 13.

  • Is there a way to force a device to choose from this folder, or to write over its variable (with java reflection? - I know that's pretty bad) to behave like normal screen not large?
  • Is there a way to maintain only two xml package (folder):
    • one for under-7inch-devices,
    • and one for above-7inch devices.
4

3 回答 3

1

However, there are devices under 3.2 which I have to support

Not very many.

Is there a way to force a device to choose from this folder, or to write over its variable (with java reflection? - I know that's pretty bad) to behave like normal screen not large?

No. However, you can find out the model numbers for those few devices and choose to load different layouts for them (e.g., R.layout.main normally, R.layout.you_are_worrying_about_this_way_too_much for few models of pre-API Level 13 7" tablets).

is there a way to maintain only two xml package (folder): one for under-7inch-devices, and one for above-7inch devices

To be safe, I would go with three:

  • res/layout/ for below-7"
  • res/layout-sw600dp/ for 7" and up
  • res/layout-xlarge/, perhaps using aliases to the ones in res/layout-sw600dp/, for the handful of XOOMs, Tab 10.1's, and such that didn't get upgraded to 3.2
于 2013-03-15T00:22:22.547 回答
1

Finally I got the best solution, I think. Somehow i didn't notice, that there are two types of aliases; the one that @Joe Malin linked, and this type.

The first type was not the solution unfortunatly, but the second one is exactly what I was looking for, and the developer page describes the solution as I needed:

res/layout/main.xml: single-pane layout

res/layout-large: multi-pane layout

layout res/layout-sw600dp: multi-pane layout

The last two files are identical, because one of them will be matched by Android 3.2 devices, and the other one is for the benefit of tablets and TVs with earlier versions of Android.

To avoid this duplication of the same file for tablets and TVs (and the maintenance headache resulting from it), you can use alias files.

So I need to make the layout-differencies in layouts with other names inside the layout folder; like layout/mainnormal.xml and layout/mainlarge.xml. Then I need to make layout.xml in different values folders, and make aliases as defined in the above link.

For example I will need a values/layout.xml a values-large-xhdpi-1280x800/layout.xml(for galaxy Note) a values-large/layout.xml (for larger devices) and maybe also a layout-sw600dp/layout.xml.

In the first two I need to create aliases for mainnormal.xml and for the others for the mainlarge.xml. This way I won't need to know in code that which type one I'm using, just use an id, for example R.layout.main.

layout.xml in the first two case:

<resources>
    <item name="main" type="layout">@layout/mainnormal</item>
</resources>

In the latter cases:

<resources>
    <item name="main" type="layout">@layout/mainlarge</item>
</resources>
于 2013-03-18T11:04:11.943 回答
0

Would making an alias resource help: Creating alias resources?

于 2013-03-12T00:17:02.390 回答