通过引用另一种样式来覆盖风味中的自定义属性
Overriding a custom attribute in a flavor by referring to another style
我在模块的同一个文件中定义了两个 cardStyle 属性,如下所示:
--> module/src/main/res/values/styles.xml
<declare-styleable name="customAttr">
<attr name="cardStyle" format="reference" />
<attr name="settingsCardStyle" format="reference" />
</declare-styleable>
<style name="CardStyle" parent="CardView">
<item name="cardCornerRadius">0dp</item>
<item name="cardElevation">0dp</item>
<item name="android:layout_marginEnd">0dp</item>
<item name="android:layout_marginStart">0dp</item>
<item name="android:layout_marginTop">0dp</item>
<item name="android:layout_marginBottom">0dp</item>
</style>
<style name="SettingsCardStyle" parent="CardView">
<item name="android:layout_marginEnd">16dp</item>
<item name="android:layout_marginStart">16dp</item>
<item name="android:layout_marginTop">16dp</item>
<item name="android:layout_marginBottom">59dp</item>
</style>
之后,我想覆盖 settingsCardStyle 中的 cardStyle,它位于我的应用程序组件中:
--> app/src/flavor/res/values/styles.xml
<style name="CardStyle" parent="CardView">
<item name="cardCornerRadius">8dp</item>
<item name="cardElevation">6dp</item>
<item name="android:layout_marginEnd">16dp</item>
<item name="android:layout_marginStart">16dp</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
</style>
<style name="SettingsCardStyle">
<item name="cardStyle">@style/CardStyle</item>
//using the following, the style works but it is redundant.
<!--<item name="cardCornerRadius">8dp</item>-->
<!--<item name="cardElevation">6dp</item>-->
<!--<item name="android:layout_marginEnd">16dp</item>-->
<!--<item name="android:layout_marginStart">16dp</item>-->
<!--<item name="android:layout_marginTop">10dp</item>-->
<!--<item name="android:layout_marginBottom">10dp</item>-->
</style>
我必须提到我有一个 CommonTheme 如下:
--> app/src/main/res/values/styles.xml
<style name="CommonTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:fontFamily">@font/open_sans</item>
<item name="fontFamily">@font/open_sans</item>
<item name="cardStyle">@style/CardStyle</item>
<item name="settingsCardStyle">@style/SettingsCardStyle</item>
</style>
但这不起作用。主应用程序识别模块中的样式,但风味应用程序不...
我做错了什么?
所以您想从风味中覆盖样式:
资源限定符允许在 app/src/flavor1/res
等
旧答案(关于模块,而不是风格)
为了在模块之间共享样式,我使用了中间样式:
常见:
<style name="_CardStyle" parent="CardView"></style>
module1(取决于共同点):
<style name="CardStyle" parent="_CardStyle"></style>
module2(取决于共同点):
<style name="CardStyle" parent="_CardStyle"></style>
您现在可以在模块中使用 CardStyle
。
我找到了解决问题的方法!
解决方案是使用继承而不是引用另一种样式。
<style name="CardStyle" parent="CardView">
<item name="cardCornerRadius">8dp</item>
<item name="cardElevation">6dp</item>
<item name="android:layout_marginEnd">16dp</item>
<item name="android:layout_marginStart">16dp</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
</style>
<style name="SettingsCardStyle" parent="CardStyle">
我在模块的同一个文件中定义了两个 cardStyle 属性,如下所示:
--> module/src/main/res/values/styles.xml
<declare-styleable name="customAttr">
<attr name="cardStyle" format="reference" />
<attr name="settingsCardStyle" format="reference" />
</declare-styleable>
<style name="CardStyle" parent="CardView">
<item name="cardCornerRadius">0dp</item>
<item name="cardElevation">0dp</item>
<item name="android:layout_marginEnd">0dp</item>
<item name="android:layout_marginStart">0dp</item>
<item name="android:layout_marginTop">0dp</item>
<item name="android:layout_marginBottom">0dp</item>
</style>
<style name="SettingsCardStyle" parent="CardView">
<item name="android:layout_marginEnd">16dp</item>
<item name="android:layout_marginStart">16dp</item>
<item name="android:layout_marginTop">16dp</item>
<item name="android:layout_marginBottom">59dp</item>
</style>
之后,我想覆盖 settingsCardStyle 中的 cardStyle,它位于我的应用程序组件中:
--> app/src/flavor/res/values/styles.xml
<style name="CardStyle" parent="CardView">
<item name="cardCornerRadius">8dp</item>
<item name="cardElevation">6dp</item>
<item name="android:layout_marginEnd">16dp</item>
<item name="android:layout_marginStart">16dp</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
</style>
<style name="SettingsCardStyle">
<item name="cardStyle">@style/CardStyle</item>
//using the following, the style works but it is redundant.
<!--<item name="cardCornerRadius">8dp</item>-->
<!--<item name="cardElevation">6dp</item>-->
<!--<item name="android:layout_marginEnd">16dp</item>-->
<!--<item name="android:layout_marginStart">16dp</item>-->
<!--<item name="android:layout_marginTop">10dp</item>-->
<!--<item name="android:layout_marginBottom">10dp</item>-->
</style>
我必须提到我有一个 CommonTheme 如下:
--> app/src/main/res/values/styles.xml
<style name="CommonTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:fontFamily">@font/open_sans</item>
<item name="fontFamily">@font/open_sans</item>
<item name="cardStyle">@style/CardStyle</item>
<item name="settingsCardStyle">@style/SettingsCardStyle</item>
</style>
但这不起作用。主应用程序识别模块中的样式,但风味应用程序不...
我做错了什么?
所以您想从风味中覆盖样式:
资源限定符允许在 app/src/flavor1/res
等
旧答案(关于模块,而不是风格)
为了在模块之间共享样式,我使用了中间样式:
常见:
<style name="_CardStyle" parent="CardView"></style>
module1(取决于共同点):
<style name="CardStyle" parent="_CardStyle"></style>
module2(取决于共同点):
<style name="CardStyle" parent="_CardStyle"></style>
您现在可以在模块中使用 CardStyle
。
我找到了解决问题的方法! 解决方案是使用继承而不是引用另一种样式。
<style name="CardStyle" parent="CardView">
<item name="cardCornerRadius">8dp</item>
<item name="cardElevation">6dp</item>
<item name="android:layout_marginEnd">16dp</item>
<item name="android:layout_marginStart">16dp</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
</style>
<style name="SettingsCardStyle" parent="CardStyle">