两个 Android 主题可以包含共同的值而不相互继承吗?

问题描述

我试图通过将功能分解为多个模块来保持我的应用程序干净。

transform_view<V,F>

我已经在 :Ux 模块的 themes.xml 中定义了我的应用程序的主题,然后在整个应用程序中使用它。

:App
:Features:Foo
:Features:Bar
:Ux

不过,我的某些功能具有自定义属性,我想添加主题中并在整个功能中使用。所以我在 :Features:Foo 和 :Features:Bar 中分别添加一个 attrs.xml 和 themes.xml。

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
   ...
</style>

<style name="Theme.MyApp.Foo" parent="Theme.MaterialComponents.Light">
   <item name="customFooViewColor">@android:color/holo_orange_dark</item>   
</style>

但现在我想在 :Features:Baz 中添加一个功能,它可以承载来自 :Features:Foo 和 :Features:Bar 的片段,因此它的主题需要包含 :Feature:Foo 中定义的自定义属性attrs.xml 和 :Feature:Bar 的 attrs.xml,以及 :Feature:Foo 的 themes.xml 和 :Feature:Bar 的 themes.xml 中定义的那些属性的相同值。

一种解决方案是将这些自定义属性及其值移动到 :Ux 模块中,但这会使用仅在几个模块中需要的特定于功能属性污染整个应用程序。

正如@ritesh4302 指出的那样,我可以通过添加对这些模块的依赖来访问 :Feature:Foo 和 :Feature:Bar 中的 attrs.xml 文件:Feature:Baz,因此另一种解决方案是复制每个模块中的所有字段:Features:Foo 和 :Features:Bar 到 :Features:Baz。

在:Features:Foo

<style name="Theme.MyApp.Bar" parent="Theme.MaterialComponents.Light">
   <item name="customBarViewColor">@android:color/holo_green_light</item>   
</style>

在:Features:Bar

<style name="Theme.MyApp.Foo" parent="Theme.MaterialComponents.Light">
   <item name="customFooViewColor">@android:color/holo_orange_dark</item>   
</style>

在:Features:Baz

<style name="Theme.MyApp.Bar" parent="Theme.MaterialComponents.Light">
   <item name="customBarViewColor">@android:color/holo_green_light</item>   
</style>

显然,这种值的重复是有问题的,因为现在我需要在多个位置更改值时更新它们。

有没有办法避免重复而不将所有内容都放在:Ux 中?有没有人写过可以实现这一点的构建插件

解决方法

如果:Feature:Baz 需要使用 :Feature:Foo 和 :Feature:Bar 编写的代码,那么您需要将它们添加为依赖项,一旦将它们添加为依赖项,您必须能够使用中定义的自定义属性基础包。