在android中为不同版本编写不同的布局

发布于 2024-12-27 18:11:03 字数 698 浏览 1 评论 0原文

我必须对 Android 应用程序进行编程,使其具有最佳兼容性,以适应不同的屏幕尺寸(平板电脑和智能手机)以及版本 2.3、3.2 和 4.0。 我知道可以将布局的文件夹命名为 res/layout-sw600dp/ 。使布局与不同的屏幕尺寸完全匹配。

因此,因为我将为平板电脑/智能手机使用完全不同的布局,所以我会从“加载屏幕”开始,它测试使用的是哪个 Android 版本以及是否使用了平板电脑或智能手机 -设备,例如在平板电脑的布局中,某些屏幕上会有智能手机上没有的按钮。

所以我必须将所有这些分开在包中,如下所示:

  • Common
  • 2.3
    • 平板电脑
    • 智能手机
  • 3.2
    • 平板电脑
    • 智能手机
  • 4.0
    • 平板电脑
    • 智能手机

我会选择这种分离,因为在我看来,这是为即将到来的 *更新*做好准备的最佳方式,如果,我们可以这么说在Android 4.0中应该在ToolBar中添加一个Button。

您认为这种分离是一件好事还是完全过载?

4.0 中还有我应该注意的其他新功能吗?

I have to program my Android app with the best compatibility to different screen sizes (tablets and smartphones) and to the versions 2.3, 3.2 and 4.0.
I know about the possibliy to name the folders of the layout like res/layout-sw600dp/. to match the layout exactly to different screen sizes.

So, cause I will use completely different layouts for Tablets / Smartphones, I would start with a "Load Screen", which tests which Android-version is used and if there is used a tablet-or smartphone-device, cause for example in the layout for the Tablet, there will be Buttons on some screens that won't be there on the smartphone.

So I will have to separate all this in packages, like this:

  • Common
  • 2.3
    • Tablet
    • Smartphone
  • 3.2
    • Tablet
    • Smartphone
  • 4.0
    • Tablet
    • Smartphone

I would choose this separation, cause it's the best way, in my opinion, to be prepared for upcoming *updates*, if, let's say there should be added a Button in the ToolBar in Android 4.0.

Is this separation a good thing in your opinion or is this a complete overload?

Are there any other new features in 4.0 that I should be aware of?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舞袖。长 2025-01-03 18:11:03

您还可以命名资源文件夹以指示版本号(例如 /res/layout-v11)或(/res/values-v13

关于您有关分离的问题 - 它实际上取决于您的具体要求。解决此问题的常见方法是回退机制:您提供一些特定的布局(例如,xlarge 横向 v11 布局)和一些要回退的通用布局。

当然,这是一个“理想化”的解决方案;但事情并不总是这样。换句话说,仅仅通过提供替代资源来实际实现一个与如此多版本的Android兼容的应用程序是非常困难的。即使您使用兼容性库(这有助于减少特定于版本的代码);您仍然需要根据运行时的版本来调整代码。

You can also name your resource folders to indicate version number (like /res/layout-v11) or (/res/values-v13)

Regarding your question about separation - it really depends on your particular requirement. A common approach to this problem is a fall-back mechanism: You provide a few specific layouts (for example, an xlarge landscape v11 layout), and a few generic ones to fall back to.

Of course, this is an "idealistic" solution; and it doesn't always work that way. In other words, it is mighty difficult to practically implement an app that is compatible with so many versions of Android solely by providing alternative resources. Even if you use the compatibility libraries (which helps a bit in reducing version-specific code); you will still have to adapt your code based on the version at run-time.

|煩躁 2025-01-03 18:11:03

您可以通过查看 Build.VERSION 找到 Android 版本。

该文档建议您根据 Build.VERSION_CODES 中的值检查 Build.VERSION.SDK_INT。

基于该版本,您可以在 if else 条件中设置布局

You can find out the Android version looking at Build.VERSION.

The documentation recommends you check Build.VERSION.SDK_INT against the values in Build.VERSION_CODES.

And based on that version you can set your layouts in if else conditions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文