[英]How do you apply a button style to all buttons in an Android app?
我使用基本活动创建了一个全新的应用程序。 我想更改所有按钮的颜色(从默认的紫色原色到自定义的非原色绿色)。 这是我的主题.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<item name="buttonStyle">@style/ButtonColour</item>
<!-- Customize your theme here. -->
</style>
<style name="Theme.MyApplication.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="ButtonColour" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">#689f38</item>
<item name="rippleColor">#c5e1a5</item>
</style>
<style name="Theme.MyApplication.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="Theme.MyApplication.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
我在此处应用ButtonColour
样式,如此处的 android 开发文档中所定义: https ://developer.android.com/guide/topics/ui/look-and-feel/themes#Widgets 当应用程序以该自定义样式运行时要将按钮更改为绿色,按钮仍然是默认的原色。
您必须使用 materialButtonStyle 而不是 buttonStyle。
<item name="materialButtonStyle">@style/ButtonColour</item>
改变
<style name="ButtonColour" parent="Widget.MaterialComponents.Button">
至
<style name="ButtonColour" parent="Theme.MyApplication">
在按钮中:
<Button style="@style/ButtonColour"
因为您已经使用 materialtheme 作为应用程序的父主题。 按钮,默认继承材质主题属性。
您可以在资源的drawable文件夹中创建一个新的布局资源文件,它会是一个形状
在这个 XML 文件中,您将能够像这样编辑您的按钮样式:`
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="@color/antique_white"/>
</shape>
` 你可以改变 <solid. 现在,您需要将此 XML 文件用作所有按钮的背景:假设您将 XML 文件命名为 button_style
android:background="@drawable/button_style"
现在您只需编辑 XML 文件,更改将应用于您的所有按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.