2.基于Android 10分析系统设置应用主题样式

基于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样式问题,为了节省读者阅读时间,删除了大部分资源的定义。

最后也建议读者通过阅读源码方式加深对此的印象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值