eclipse 如何在 Android Layout 文件夹中定位 1280x720 WXGA720 分辨率(如新的 Galaxy Nexus)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9006494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I target 1280x720 WXGA720 resolution (like new Galaxy Nexus) in Android Layout folders?
提问by Matt W
When testing my application at WXGA720 resolution on the Android Emulator running 4.0.3 Ice Cream Sandwich, my app takes the layout assets from the layout-normal-land-854x480 folder in landscape view and the layout-port-480x320 folder for portrait view. I would like to be able to specifically target this resolution so my app displays properly on the new phones. How do I do this? I have tried the following...
在运行 4.0.3 Ice Cream Sandwich 的 Android 模拟器上以 WXGA720 分辨率测试我的应用程序时,我的应用程序从横向视图中的 layout-normal-land-854x480 文件夹和纵向视图中的 layout-port-480x320 文件夹中获取布局资产。我希望能够专门针对此分辨率,以便我的应用程序在新手机上正确显示。我该怎么做呢?我尝试了以下...
layout-normal-land-xhdpi
layout-normal-port-xhdpi
layout-normal-land-xlarge
layout-normal-land-large
layout-normal-land-1280x720
layout-normal-port-1280x720, etc...
layout-normal-land-xhdpi
layout-normal-port-xhdpi
layout-normal-land-xlarge
layout-normal-land-large
layout-normal-land-1280x720
layout-normal-port-1280x720 等...
...and it still takes the assets from the wrong folders detailed above.
...它仍然从上面详述的错误文件夹中获取资产。
My app is targeting Android 2.1 (which I assume is still the standard target these days), so I can't as far as I know use the new layout qualifiers. Is this a bug? Has anyone had this same issue and found a workaround?
我的应用程序的目标是 Android 2.1(我认为现在仍然是标准目标),所以据我所知,我不能使用新的布局限定符。这是一个错误吗?有没有人遇到过同样的问题并找到了解决方法?
I have the following folder configuration, and all of the other AVDs display as expected:
我有以下文件夹配置,所有其他 AVD 都按预期显示:
layout
layout-land
layout-normal-land-480x320
layout-normal-land-854x480
layout-port-480x320
layout-port-800x480
布局
layout-land
layout-normal-land-480x320
layout-normal-land-854x480
layout-port-480x320
layout-port-800x480
回答by CommonsWare
When testing my application at WGXA720 resolution on the Android Emulator running 4.0.3 Ice Cream Sandwich, my app takes the layout assets from the layout-normal-land-854x480 folder in landscape view and the layout-port-480x320 folder for portrait view.
在运行 4.0.3 Ice Cream Sandwich 的 Android 模拟器上以 WGXA720 分辨率测试我的应用程序时,我的应用程序从横向视图中的 layout-normal-land-854x480 文件夹和纵向视图中的 layout-port-480x320 文件夹中获取布局资产。
If I had to guess, you do not have a valid <supports-screens>
element (or possibly no android:minSdkVersion
) in your manifest, and so you are being thrown into a compatibility mode.
如果我不得不猜测,您的清单中<supports-screens>
没有有效的元素(或可能没有android:minSdkVersion
),因此您将陷入兼容模式。
Also, please don't use suffixes like -480x320
and -854x480
. These were removed from the documentation for a reason. Whatever problem you think you are solving with them can be better solved some other way.
另外,请不要使用像-480x320
和这样的后缀-854x480
。出于某种原因,这些已从文档中删除。无论您认为您正在与他们解决什么问题,都可以通过其他方式更好地解决。
My app is targeting Android 2.1 (which I assume is still the standard target these days), so I can't as far as I know use the new layout qualifiers.
我的应用程序的目标是 Android 2.1(我认为现在仍然是标准目标),所以据我所知,我不能使用新的布局限定符。
You can set your build target to a higher level and use the new layout qualifiers. However, older devices will ignore them, so you will still need to use -normal
and -land
and kin for them, in parallel with any resource sets with the new qualifiers. You can create resource aliases to minimize code duplication.
您可以将构建目标设置为更高级别并使用新的布局限定符。但是,较旧的设备会忽略它们,因此您仍然需要对它们使用-normal
and-land
和 kin,同时使用具有新限定符的任何资源集。您可以创建资源别名以最大程度地减少代码重复。
回答by Tary
I was having a similar problem displaying my layouts properly but I was using the HTC Rezound that has 1280 by 720 resolution. It uses Android version 2.3.4. My screens were only showing on the upper portion of the display and the bottom third was black. Everything was displaying properly using lower resolution phones.
我在正确显示布局时遇到了类似的问题,但我使用的是 1280 x 720 分辨率的 HTC Rezound。它使用 Android 版本 2.3.4。我的屏幕只显示在显示器的上部,底部的三分之一是黑色的。使用分辨率较低的手机,一切都正常显示。
I went through the exercise of making new layout directories ( layout-xhdpi, layout-large, layout-1280x720, layout-1200x700, etc) and modifying my layout files to match the larger resolution, but each attempt failed and the app always showed in the upper part of the screen only. I had read the docs from http://developer.android.com/guide/practices/screens_support.htmland did not find anything that solved the problem. I finally fixed it, and the solution had nothing whatsoever to with adding new layout directories set up for higher resolutions! The solution was all about the "uses-sdk" statement in the AndroidManifest.xml file. My original manifest contained the statement:
我经历了创建新布局目录(layout-xhdpi、layout-large、layout-1280x720、layout-1200x700 等)并修改我的布局文件以匹配更大分辨率的练习,但每次尝试都失败了,应用程序总是显示在仅屏幕上部。我已经阅读了http://developer.android.com/guide/practices/screens_support.html的文档 ,但没有找到任何解决问题的方法。我终于修复了它,该解决方案与添加为更高分辨率设置的新布局目录无关!解决方案是关于 AndroidManifest.xml 文件中的“uses-sdk”语句。我的原始清单包含以下声明:
<uses-sdk minSdkVersion="8"/>
With only the minimum sdk version specified, the graphics would not display properly on the Rezound. However, after changing only one line in the AndroidManifest.xml (and making no other changes) the phone displayed all the screens properly:
仅指定最低 sdk 版本时,图形将无法在 Rezound 上正确显示。但是,在仅更改 AndroidManifest.xml 中的一行(并且未进行其他更改)后,手机正确显示了所有屏幕:
<uses-sdk android:targetSdkVersion="9" minSdkVersion="8" />
Why did this fix it but not the documented methods? I don't know! I am finding many hidden connections in Android that seem to defy a logical explanation. Can someone explain to me why changing the uses-sdk statement is critical to displaying on a higher resolution display?
为什么这可以修复它而不是记录的方法?我不知道!我在 Android 中发现许多隐藏的连接似乎无视逻辑解释。有人可以向我解释为什么更改 uses-sdk 语句对于在更高分辨率的显示器上显示至关重要吗?
回答by Tary
I had answered this question before but my previous response only allowed me to fill the display (it only filled the top third before) but I could never get an alternate layout to be used. I am using an HTC Rezound which has a 1280 by 720 display. I needed to find out what kind of display Android thinks that it is. I added the following code in my onCreate handler:
我之前回答过这个问题,但我之前的回答只允许我填充显示(之前它只填充了前三分之一),但我永远无法使用替代布局。我正在使用具有 1280 x 720 显示屏的 HTC Rezound。我需要找出 Android 认为它是什么类型的显示器。我在 onCreate 处理程序中添加了以下代码:
// Figure out what kind of display we have
int screenLayout = getResources().getConfiguration().screenLayout;
if ((screenLayout & Configuration.SCREENLAYOUT_SIZE_SMALL) == Configuration.SCREENLAYOUT_SIZE_SMALL)
LogMessage("Main onCreate", "Info", "Screen size is Small");
else if ((screenLayout & Configuration.SCREENLAYOUT_SIZE_NORMAL) == Configuration.SCREENLAYOUT_SIZE_NORMAL)
LogMessage("Main onCreate", "Info", "Screen size is Normal");
else if ((screenLayout & Configuration.SCREENLAYOUT_SIZE_LARGE) == Configuration.SCREENLAYOUT_SIZE_LARGE)
LogMessage("Main onCreate", "Info", "Screen size is Large");
if ((screenLayout & Configuration.SCREENLAYOUT_LONG_YES) == Configuration.SCREENLAYOUT_LONG_YES)
LogMessage("Main onCreate", "Info", "Screen size is Long");
// Get the metrics
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int heightPixels = metrics.heightPixels;
int widthPixels = metrics.widthPixels;
int densityDpi = metrics.densityDpi;
float density = metrics.density;
float scaledDensity = metrics.scaledDensity;
float xdpi = metrics.xdpi;
float ydpi = metrics.ydpi;
LogMessage("Main onCreate", "Info", "Screen W x H pixels: " + widthPixels + " x " + heightPixels);
LogMessage("Main onCreate", "Info", "Screen X x Y dpi: " + xdpi + " x " + ydpi);
LogMessage("Main onCreate", "Info", "density = " + density + " scaledDensity = " + scaledDensity +
" densityDpi = " + densityDpi);
And the results in the logs were:
日志中的结果是:
Info, Main onCreate, Screen size is Normal
Info, Main onCreate, Screen size is Long
Info, Main onCreate, Screen W x H pixels: 720 x 1280
Info, Main onCreate, Screen X x Y dpi: 345.0566 x 342.23157
Info, Main onCreate, density = 2.0 scaledDensity = 2.0 densityDpi = 320
With this I realized that Android was calling this a normal display so I created a res / layout-normal-1280x720 directory with my alternate layout files.
有了这个,我意识到 Android 将其称为正常显示,因此我使用备用布局文件创建了一个 res / layout-normal-1280x720 目录。
My manifest contained:
我的清单包含:
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"
/>
<uses-sdk android:targetSdkVersion="9" android:minSdkVersion="8" />
With these changes I was finally able to get an alternate layout to be used with this phone.
通过这些更改,我终于能够获得与这款手机一起使用的替代布局。