Program different layouts for different versions in android

喜夏-厌秋 提交于 2019-11-28 00:24:56

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/8908508/program-different-layouts-for-different-versions-in-android

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