如何为应用程序使用设备默认主题? [英] How to use device default theme for app?

查看:26
本文介绍了如何为应用程序使用设备默认主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 Android 2.1、2.2、2.3.3、3.0/3.1 和 4.0.x 开发一个应用程序.在这些 API 级别之间,大约有 3 种不同类型的主题.最新的 4.0.x 主题是 Holo.

I have an app that I want to develop for Android 2.1, 2.2, 2.3.3, 3.0/3.1 and 4.0.x. Between those API levels, there are about 3 different types of Themes. The latest 4.0.x theme is Holo.

无论如何,我希望我的应用程序在任何设备上使用默认的 Android 主题.如果它在 4.0.x 设备上,我希望它使用 Holo.如果它在 2.3.3 上,我希望它使用该版本的 Android 的主题.等等...

Anyways, I want my app to use the default Android theme for whatever device it's on. If it's on a 4.0.x device, I want it to use Holo. If it's on 2.3.3 I want it to use the theme from that version of Android. etc...

有没有简单的方法来解决这个问题?我注意到在 4.0 中,他们添加了一个您可以使用的 Theme.DeviceDefault 主题,但这对较旧的 API 级别没有帮助.最好的方法是什么?

Is there a simple way to approach this? I noticed that with 4.0, they added a Theme.DeviceDefault theme that you can use, but this doesn't help me for older API levels. What is the best approach to this?

推荐答案

目前有多达 3 个,有时 4 个主题可用于 Android 设备(.Light 变体和类似的不包括在内)

There are currently up to 3, sometimes 4 Themes available for Android devices (.Light variations and similar not included)

Android 2.3 Gingerbread(10) 以下版本的默认设置,包括这些版本中的一些小的样式更改

the default for the earliest versions of Android up to 2.3 Gingerbread(10), including some minor style changes in those versions

随 Android 3.0 Honeycomb (11) 引入

introduced with Android 3.0 Honeycomb (11)

Android 5.0 Lollipop 新功能 (21)

new in Android 5.0 Lollipop (21)

(可以是任何东西)

随 4.0 Ice Cream Sandwich (14) 引入,该主题可由设备制造商自定义.它代表设备的本机外观——即 Nexus 设备上的 Holo 或 Material(在 Android Studio 的设计编辑器中),也可能是其他设备上的自定义外观.万一定制的东西"不是一种选择,这些设备必须带有股票主题.想要股票主题的应用必须指定它.

introduced with 4.0 Ice Cream Sandwich (14), a theme that can be customized by the device manufacturer. It represents the native look of the device - i.e. Holo or Material on Nexus devices (& in Android Studio's design editor), maybe something custom on other devices. In case "something custom" isn't an option, those devices must come with the stock themes. Apps that want the stock theme have to specify it though.

最好的方法是什么?

无主题 + targetSdkVersion >= 14

最简单但不一定是最好的选择是根本不定义主题.然后,Android 将为您选择默认值.但是 Android 不想让您的应用程序使用您不期望的主题感到惊讶,因此它会退回到您可能为应用程序设计的主题.它通过查看 AndroidManifest.xml 中的 android:targetSdkVersion(现在可以通过 gradle 设置)来实现.

No theme + targetSdkVersion >= 14

The simplest, but not necessarily best option is to define no theme at all. Android will then select the default for you. But Android does not want to surprise your app with themes you're not expecting so it falls back to the Theme you probably had designed your app for. It does so by looking at the android:targetSdkVersion within AndroidManifest.xml (which can nowadays be set via gradle).

  • 针对只有 Theme(即 API 级别 3-10)的旧平台的应用将仅获得 Theme.
  • 面向 11-13 岁的应用获得 Theme.Holo.
  • 14 或以上将获得 Theme.DeviceDefault.
  • Apps that target old platforms, which had only Theme (i.e. API levels 3-10), will get only Theme.
  • Apps targeting 11-13 get Theme.Holo.
  • 14 or above will get Theme.DeviceDefault.

因为这只是为了向后兼容,所以您不会在旧的 Gingerbread 手机上获得 Theme.Material.因此没有主题 + 目标 14+ = 设备默认.

Since this is just for backwards compatibility, you won't get Theme.Material on your old Gingerbread phone. Therefore no theme + target 14+ = device default.

Android 的资源覆盖系统允许根据设备 API 级别指定样式.例如,res/values-v11res/values-v21 中样式的不同版本.这也是任何通过 Android Studio 新创建的应用程序都会为您设置的内容.

Android's resource overlay system allows to specify styles based on device API level. For example different versions of a style in res/values-v11 and res/values-v21. This is also what any newly created apps via Android Studio will setup for you.

例如,.Light 主题应用的最基本设置如下所示:

As an example, the most basic setup for a .Light themed app looks like this:

/res/values/styles.xml 适用于每台设备并作为基础

/res/values/styles.xml is applied to every device and serves as base

<resources>
    <style name="AppTheme" parent="android:Theme.Light"/>
</resources>


/res/values-v11/styles.xml 加载到所有 API 级别为 11 及以上(包括 21 及以上)的设备上.但只是最新版本的AppTheme"实际使用.


/res/values-v11/styles.xml is loaded on all devices that have API level 11 and above (including those that are 21 and above). But just the newest version of "AppTheme" is actually used.

<resources>
    <style name="AppTheme" parent="android:Theme.Holo.Light"/>
</resources>


/res/values-v21/styles.xml

<resources>
    <style name="AppTheme" parent="android:Theme.Material.Light"/>
</resources>

注意:或者在 /res/values-v14/styles.xml 中指定 Theme.DeviceDefault 应该足以拥有默认外观,但这不允许微调设计.添加 v14 覆盖不会有什么坏处.DeviceDefault 和 Holo 毕竟可能不同.

Note: alternatively specifying Theme.DeviceDefault in /res/values-v14/styles.xml should be enough for having a default look but that doesn't allow to fine tune the design. Doesn't hurt to add the v14 override. DeviceDefault and Holo could be different after all.

AndroidManifest.xml 是使用自定义主题的地方.例如.作为应用程序范围的默认值:

AndroidManifest.xml is the place where the custom theme is put to use. E.g. as application wide default:

...
<application
    android:theme="@style/AppTheme"
    ...


官方文档链接

根据平台版本选择主题 - 官方文档用于通过资源配置定义不同的样式


Links to official documentation

Select a theme based on platform version - Official doc for defining different styles via resource configurations

Holo Everywhere - 解释默认机制的博文和 DeviceDefault/Holo 主题

Holo Everywhere - blog post that explains the default mechanism and DeviceDefault / Holo theme

使用材料主题 - 材料文档

这篇关于如何为应用程序使用设备默认主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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