gpt4 book ai didi

android - 如何提供替代布局资源

转载 作者:行者123 更新时间:2023-11-29 16:13:36 26 4
gpt4 key购买 nike

我有一个要求,我必须为我的应用程序提供不同的 layout 资源。

此应用程序应该在多种屏幕尺寸上运行(一如既往),我希望能够尽可能准确地解决其中的许多问题。

截至目前,我的目标屏幕尺寸如下:

  • 1280x800
  • 800x480
  • 800x600
  • 1024x600

为此,我创建了这样的布局结构:

Layout structure

在 XML 中,我声明了以下支持:

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

我正在模拟器中测试,问题是:它接受 layout-w480dp-land xml,但是当我进入纵向模式时,它会退回到默认布局。

知道我可能哪里出错了吗?

最佳答案

正如我在 Android Chatroom 中所说,PixelsDensity-independent pixel (dp) 是不同的:

Density-independent pixel (dp)

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.

当您有一个名为 layout-w480dp 的文件夹时,您的意思是:

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

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

dp = px / (dpi / 160)

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

关于android - 如何提供替代布局资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11188531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com