如何提供替代布局资源 [英] How to provide alternative layout resources

查看:84
本文介绍了如何提供替代布局资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我必须提供不同的布局资源,我的申请。

I have a requirement where I have to provide different layout resources with my application.

此应用程序应该在许多屏幕尺寸上运行(一如既往),我希望能够解决其中不乏像precisely越好。

This application is supposed to run on many screen sizes (as always) and I want to be able to address many of them as precisely as possible.

截至目前,我正在针对以下屏幕尺寸:

As of now, I am targeting following screen sizes:


  • 1280×800

  • 800×480

  • 800×600

  • 1024×600

对于这一点,我创建了一个布局结构是这样的:

For this, I created a layout structure like this:

和在XML中,我已经宣布了以下支持:

And in XML, I have declared the following support:

<supports-screens android:resizeable="true"
              android:smallScreens="true"
              android:normalScreens="true"
              android:largeScreens="true"
              android:xlargeScreens="true"
              android:anyDensity="true"/>

我在呢模拟器测试,问题是:它接受布局w480dp土地 XML,但是当我在纵向模式走,它会回到默认布局

I am testing in it emulator and the problem is: it accepts layout-w480dp-land xml, but when I go in portrait mode, it falls back to default layout.

任何想法,我可能会错误?

Any idea where I might be going wrong?

推荐答案

我在的Andr​​oid 聊天室,像素称密度独立像素(DP)是不同的:

As I said in the Android Chatroom, Pixels and Density-independent pixel (dp) are different:

密度独立像素(DP)

一个虚拟的像素单位,你应该
  定义UI布局时使用,给前preSS布局的尺寸或位置
  在密度无关的方式。密度独立像素
  相当于一个物理象素160 dpi的屏幕,它是上
  基准密度系统为中等密度屏幕假设。
  在运行时,系统透明地处理DP的任何缩放
  单元,如需要,根据所使用的屏幕的实际密度。
  DP的单位屏幕像素转换很简单:像素= DP *(DPI
  / 160)。例如,一个240 dpi的屏幕上,1 DP等于1.5的物理
  像素。定义时,您应该总是使用dp单位的
  应用程序的用户界面,以确保与屏幕UI的正确显示
  不同的密度。

A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way. The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.

当你有一个文件夹命名布局w480dp你的意思是:

When you have a folder named layout-w480dp what you're saying is:

,只要使用此布局文件夹的宽度尺寸等于或大于480的 DP 更大。

"Use this layout folder as long as the width dimension is equal to or greater than 480 dp."

现在,如果你有一个240dpi设备与屏幕尺寸480x800像素(这是非常有可能),那么你实际上只拥有 320dp 玩 - 解释为什么你的文件夹现在正在跳过。

Now, if you have a device with 240dpi with a screen size 480x800 pixels (which is very likely) then you actually only have 320dp to play with - explaining why you're folder is now being skipped.

DP = PX /(DPI / 160)

dp = px / (dpi / 160)

320dp = 480像素/(240dpi / 160)

320dp = 480px / (240dpi / 160)

这篇关于如何提供替代布局资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆