java - 如何在 Android Studio 项目的应用程序主题中嵌套按钮样式元素
问题描述
我正在尝试将 Material Button 背景颜色定义为我的 Android 应用程序中的主题,以便所有按钮都从应用程序主题中获取样式。我在我的 Android 应用程序中定义了以下样式。
我的主题/样式 xml 文件中有以下内容
<style name="Theme.SimpleTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Some theming -->
</style>
<style name="SimpleButton" parent="Widget.MaterialComponents.Button">
<item name="colorPrimary">@color/blue_grey_800</item>
<item name="colorOnPrimary">@color/white</item>
</style>
然后在布局文件中,我有:
<com.google.android.material.button.MaterialButton
android:theme="@style/SimpleButton" />
现在,这可以使特定按钮具有我想要的背景颜色,但是我必须在所有布局文件中的所有材质按钮元素中设置 Style 属性。
有什么方法可以嵌套样式,这样我就不需要在所有按钮中明确提及样式?
解决方案
是的,有办法做到这一点。
首先,为您的按钮创建一个主题,并使其继承 AppCompat 按钮(虽然它是一个材质按钮)。例如:
<style name="bTheme" parent="Widget.AppCompat.Button">
<item name="android:backgroundTint">@color/black</item>
<item name="android:textColor">@color/white</item>
</style>
然后,在主应用程序的主题上,添加以下行:
<item name="materialButtonStyle"> @style/bTheme</item>
这应该可以解决问题:)
推荐阅读
- java - 如何在 Java 中的 Stream 中查找重复元素
- wordpress - Compute Engine(wordpress 网站)崩溃、错误的 SSL 命令和空间问题
- php - 按层次结构组织 URL 数组
- openshift - 通过代理访问隔离网络上的 IPI 安装
- r - 使用 R 在循环中访问索引数据
- matlab - 如何将网格拟合到一组点中的某些点?
- javascript - React Image Mapper - 单击时更改状态,并立即进行 API 调用
- c# - 是否可以使用正则表达式从整个字符串中删除每个出现的字符及其后的一个字符?
- wordpress - Wordpress如何在执行cron之前注册全局对象,以便在cron作业期间可用?
- openapi - 如何在不使用 confluence 插件的情况下将 swagger/openapi yaml 文件包含到 doctoolchain 中并获得类似 swagger 的输出?