You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 05:24:04