在包含库项目的android项目中,我需要在manifest中为主应用程序和库设置不同的主题。好像我需要在主项目中设置以下主题
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/grey</item>
<item name="colorPrimaryDark">@color/black</item>
<item name="colorAccent">@color/green</item>
</style>
下面是图书馆项目
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/blue</item>
<item name="colorPrimaryDark">@color/red</item>
<item name="colorAccent">@color/white</item>
</style>
库项目样式由主应用程序样式替代。
当我给不同的样式命名时,它会抛出明显的合并冲突错误。
以下是主应用程序的清单
<application
android:name=".MyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
这个是给图书馆的
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
>
最佳答案
构建应用程序后,Gradle会将清单文件合并为一个文件,因此将库中的主题设置为application
标记将被应用程序的属性覆盖。
将样式名更改为其他名称,
<style name="MyLibTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/blue</item>
<item name="colorPrimaryDark">@color/red</item>
<item name="colorAccent">@color/white</item>
</style>
如果要在库函数中启动某些活动,请在库清单文件中将主题设置为“单独的活动”。在项目生成后合并时,库清单上的
application
标记上设置的主题将被替换,因此请为单独的活动设置主题 <activity android:name=".MyLibActivity" android:theme="@style/MyLibTheme">
</activity>
或者可以在活动代码中使用setTheme。
setTheme(R.style.MyLibTheme);
在构建过程中,如果用户(库用户)使用相同的主题名,则它将替换为main
app
模块主题,以保护这种情况,您可以在库的渐变文件中使用渐变选项。android{
// replace mylib_ with name related to your library
resourcePrefix 'mylib_'
}