在您的库模块中的 xml 布局引用 ?attr/colorPrimary 而不是 @color/AppPrimary
这引用了应用主题中的调色板属性。 IE。;
<item name="colorPrimary">@color/AppPrimary</item>
您还可以参考其他调色板属性,如:
<item name="colorPrimaryDark">...</item> -> ?attr/colorPrimaryDark
<item name="colorAccent">...</item> -> ?attr/colorAccent
等等。
Please refer to this documentation
警告:
这些示例引用了主题中的 AppCompat 实现。即没有 android: 命名空间前缀。
-- 编辑--
简短的回答是,您根本无法从引用库 colors.xml 文件的主模块中“获取”定义的颜色。
但是,您可以在主题中利用自定义属性来实现相同的效果。
这可以通过在库模块的attrs.xml 文件中声明一个通用样式来实现,如下所示:
<declare-styleable name="BaseTheme">
<attr name="someColor" format="reference|color" />
</declare-styleable>
注意:样式的名称与此实现无关,因此请随意命名。
然后在“style.xml”或“theme.xml”文件中的主模块主题定义中,您需要将此属性设置为您要共享的颜色,如下所示:
<style name="Theme.Example" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Color Palette -->
<item name="colorPrimary">...</item>
<item name="colorPrimaryDark">...</item>
<item name="colorAccent">...</item>
<!-- Here we define our custom attributes -->
<item name="someColor">@color/dark_blue</item>
</style>
定义后,您可以在库模块 XML 代码中引用该属性。例如;
<TextView
...
android:textColor="?attr/someColor"
/>