修改themes.xml中主题颜色属性后App无变化问题求助
自定义App主题不生效的解决办法
你遇到的问题核心是没把自定义主题应用到App中,再加上XML代码存在语法错误,导致主题配置完全没起作用。下面是具体修复步骤:
1. 让App使用自定义主题
在Manifest.xml的<application>标签里,把android:theme的值改成你自定义的Theme.MiPlan——原来的Theme.MaterialComponents.DayNight是系统默认主题,你的修改自然不会生效:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MiPlan" <!-- 修改此处 --> ...>
2. 修复所有XML语法错误
两份XML代码都存在标签未闭合的问题,这会导致Android编译时无法正确解析配置,必须全部修复:
修复后的Manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.miplan"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MiPlan"> <activity android:name=".SplashActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity" android:exported="false" /> <activity android:name=".LogoActivity" android:exported="true"> </activity> </application> </manifest>
修复后的themes.xml
<resources> <!-- Base application theme. --> <style name="Theme.MiPlan" parent="Theme.MaterialComponents.DayNight.DarkActionBar"> <item name="colorPrimary">#458077</item> <item name="colorPrimaryDark">#620051</item> <item name="android:textColorPrimary">#FD1CC1</item> <item name="android:windowBackground">#FFFFFF</item> <!-- 原#FFFF为无效颜色值,修正为标准6位十六进制 --> <item name="android:navigationBarColor">#FF2255</item> <!-- 原#FF25为无效颜色值,修正为标准6位十六进制 --> </style> </resources>
额外注意:你写的#FFFF和#FF25属于不合法的颜色值,Android要求颜色值为6位(或8位带透明度)十六进制格式,已帮你修正为合法值,避免出现显示异常。
修复完成后重新编译运行App,自定义的主题属性就会正常生效了。
内容的提问来源于stack exchange,提问作者Amirhussein
相关产品推荐
相关产品推荐

