如何使用设备的默认主题的应用程序?如何使用、应用程序、主题、设备

2023-09-12 04:07:21 作者:孤心大叔.

我有一个应用程序,我想开发的Andr​​oid 2.1,2.2,2.3.3,3.0 / 3.1和4.0.x的这些API级别之间,大概有3种不同类型的主题的。最新的4.0.x的主题是全息。

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.

不管怎样,我想我的应用程序使用默认的Andr​​oid主题为任何设备是上。如果它是一个4.0.x的设备上,我希望它使用全息。如果它是在2.3.3我想它使用的主题从该版本的Andr​​oid。等等...

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姜饼(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引入蜂窝(11)

introduced with Android 3.0 Honeycomb (11)

小米4我下载了一个主题 把所有应用程序的图标都换了 怎么把这些图标换回系统默认 主题不换 只换图标

在新的Andr​​oid 5.0棒棒堂(21)

new in Android 5.0 Lollipop (21)

(可以是任何东西)

4.0冰淇淋三明治(14),可以由设备制造商进行定制主题介绍。它重新presents设备的本机外观 - 即全息或物质上的Nexus设备(功放;在机器人工作室的设计编辑器),或许真的定制在其他设备上。如果自定义的东西是不是一种选择,这些设备必须配股票的主题。希望股票主题的应用程序必须虽然指定它。

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.

什么是最好的方法呢?

的最简单的,但不一定最好的选择是没有定义的主题都没有。 Android将然后选择默认为您服务。但是Android不希望与主题,你不希望因此回落到你可能设计了您的应用程序的主题惊喜你的应用程序。它通过在看安卓targetSdkVersion 的Andr​​oidManifest.xml (可如今可以通过摇篮设置)。

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).

应用程序针对老平台,其中只有主题(即API等级3-10),将只得到主题。 应用程序针对11-13 GET 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.

由于这仅仅是为了向下兼容,您将无法获得 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 /值-V11例如不同版本 RES /值-V21 。这也是为什么通过Android的工作室任何新创建的应用程序将设置你。

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和全息可能是毕竟不同。

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.

的Andr​​oidManifest.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

全息到处 - 博客文章,解释了默认的机制和DeviceDefault /霍洛主题

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

使用材料主题 - 材料文件