How do I target 1280x720 WXGA720 resolution (like new Galaxy Nexus) in Android Layout folders?

柔情痞子 提交于 2019-12-03 15:19:58

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.

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.

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.

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.

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.

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.

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.html and 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:

<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:

<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?

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:

// 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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!