如何在Android Studio项目应用主题中嵌套Material Button样式元素
解决方案
你直接在应用的全局主题中指定Material Button的默认样式即可,无需逐个给控件声明style属性。
第一步:修改全局主题配置
在你的styles.xml的主主题中新增materialButtonStyle配置项,关联你自定义的按钮样式:
<style name="Theme.SimpleTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar"> <!-- 原有其他主题配置 --> <item name="materialButtonStyle">@style/SimpleButton</item> </style> <style name="SimpleButton" parent="Widget.MaterialComponents.Button"> <item name="colorPrimary">@color/blue_grey_800</item> <item name="colorOnPrimary">@color/white</item> </style>
注意:
materialButtonStyle属性不要加android:前缀,它是Material Components库提供的自定义属性。
第二步:移除布局中冗余的样式声明
你可以删掉所有MaterialButton控件上单独设置的android:theme="@style/SimpleButton"属性,全局所有Material Button都会自动应用你定义的SimpleButton样式。
补充说明
如果后续有个别按钮需要使用特殊样式,只要给对应按钮单独设置style属性即可覆盖全局默认样式。
内容的提问来源于stack exchange,提问作者Shahid Thaika
相关产品推荐
相关产品推荐

