基于Android 10分析系统设置应用主题样式
相关源码文件如下:
packages/apps/Settings/
- AndroidManifest.xml
packages/apps/Settings/res/values
- themes.xml
frameworks/base/core/res/res/values
- themes_device_defaults.xml
- themes_material.xml
- themes.xml
frameworks/base/core/res/res/values-night
- themes_device_defaults.xml
设置APK资源
首先来看看设置apk的AndroidManifest.xml文件
路径:/packages/apps/Settings/AndroidManifest.xml
<application android:label="@string/settings_label"
android:icon="@drawable/ic_launcher_settings"
android:theme="@style/Theme.Settings"
android:hardwareAccelerated="true"
android:requiredForAllUsers="true"
android:supportsRtl="true"
android:backupAgent="com.android.settings.backup.SettingsBackupHelper"
android:usesCleartextTraffic="true"
android:defaultToDeviceProtectedStorage="true"
android:directBootAware="true"
android:appComponentFactory="androidx.core.app.CoreComponentFactory">
可以看见theme属性为:@style/Theme.Settings 定义在themes.xml文件中。
路径:/packages/apps/Settings/res/values/themes.xml
<style name="Theme.SettingsBase" parent="@android:style/Theme.DeviceDefault.Settings" />
<style name="Theme.Settings" parent="Theme.SettingsBase">
<!-- ... -->
</style>
最后继承关系是 Theme.Settings > Theme.SettingsBase > @android:style/Theme.DeviceDefault.Settings
@android表示引用Android fraamework-res.apk系统资源,所以接下来看看framework-res内容。
系统内部资源
日间主题
路径: /frameworks/base/core/res/res/values/themes_device_defaults.xml
<!-- DeviceDefault theme for a window that should look like the Settings app. -->
<style name="Theme.DeviceDefault.Settings" parent="Theme.DeviceDefault.Light">
<!-- ... -->
</style>
<!-- Variant of {@link #Theme_DeviceDefault} with a light-colored style -->
<style name="Theme.DeviceDefault.Light" parent="Theme.Material.Light" >
<!-- ... -->
</style>
路径:/frameworks/base/core/res/res/values/themes_material.xml
<!-- Material theme (light version). -->
<style name="Theme.Material.Light" parent="Theme.Light">
<!-- ... -->
<!-- Window attributes -->
<item name="windowBackground">?attr/colorBackground</item>
<!-- ... -->
</style>
路径:/frameworks/base/core/res/res/values/themes.xml
<!-- Theme for a light background with dark text on top. Set your activity
to this theme if you would like such an appearance. As with the
default theme, you should try to assume little more than that the
background will be a light color.
<p>This is designed for API level 10 and lower.</p>-->
<style name="Theme.Light">
<!-- ... -->
<item name="colorBackground">@color/background_light</item>
<!-- ... -->
</style>
夜间主题
路径:/frameworks/base/core/res/res/values-night/themes_device_defaults.xml
<!-- DeviceDefault theme for a window that should look like the Settings app. -->
<style name="Theme.DeviceDefault.Settings" parent="Theme.DeviceDefault">
<!-- ... -->
</style>
路径:/frameworks/base/core/res/res/values/themes_device_defaults.xml
<style name="Theme.DeviceDefault" parent="Theme.DeviceDefaultBase" />
<style name="Theme.DeviceDefaultBase" parent="Theme.Material" >
<!-- ... -->
</style>
路径:/frameworks/base/core/res/res/values/themes_material.xml
<style name="Theme.Material">
<!-- ... -->
<item name="colorBackground">@color/background_material_dark</item>
<!-- Window attributes -->
<item name="windowBackground">?attr/colorBackground</item>
<!-- ... -->
</style>
总结
Light
Theme.Settings
> Theme.SettingsBase
> Theme.DeviceDefault.Settings
> Theme.DeviceDefault.Light
> Theme.Material.Light
<item name="windowBackground">?attr/colorBackground</item>
<item name="colorBackground">@color/background_material_light</item>
<color name="background_material_light">@color/material_grey_50</color>
<color name="material_grey_50">#fffafafa</color>
> Theme.Light
Night
Theme.Settings
> Theme.SettingsBase
> Theme.DeviceDefault.Settings
> Theme.DeviceDefault
> Theme.DeviceDefaultBase
> Theme.Material
<item name="windowBackground">?attr/colorBackground</item>
<item name="colorBackground">@color/background_material_dark</item>
<color name="background_material_dark">@color/material_grey_850</color>
<color name="material_grey_850">#ff303030</color>
写此文章目的是为了快速定位到系统资源,方便解决App UI样式问题,为了节省读者阅读时间,删除了大部分资源的定义。
最后也建议读者通过阅读源码方式加深对此的印象。