我正在使用最新的 Android Studio 版本来编写我的应用程序,但我无法更改按钮样式。 似乎在生成新项目时,它已经具有一些我似乎无法覆盖的默认按钮样式。
每次我放置一个平面按钮时,它已经有一些背景颜色(“colorPrimary”),如果我尝试应用样式,它只会更改按钮的文本,但不会更改形状或颜色。
这是我的风格:
<style name="buttonStartStyle">
<item name="android:background">@drawable/button_style1</item>
<item name="android:layout_margin">20dp</item>
<item name="android:textColor">@color/white</item>
<item name="android:textAllCaps">false</item>
<item name="android:textSize">18sp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
和我的button_style1.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="50dp"/>
<solid android:color="@color/smoothWhite"/>
<size android:height="50dp"/>
<gradient android:startColor="@color/green_card" android:endColor="@color/green_main"/>
</shape>
</item>
我的按钮 xml 代码:
<Button
android:id="@+id/button"
style="@style/buttonStartStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="121dp"
tools:layout_editor_absoluteY="244dp" />
这是我的主题.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.B_app" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/green_dark</item>
<item name="colorPrimaryVariant">@color/black</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>
<!-- Customize your theme here. -->
</style>
<style name="Theme.B_app.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="Theme.B_app.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="Theme.B_app.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="buttonStartStyle">
<item name="android:background">@drawable/button_style1</item>
<item name="android:layout_margin">20dp</item>
<item name="android:textColor">@color/white</item>
<item name="android:textAllCaps">false</item>
<item name="android:textSize">18sp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="testStyle">
<item name="android:background">@drawable/button_style2</item>
<item name="android:layout_margin">20dp</item>
<item name="android:textColor">@color/white</item>
<item name="android:textAllCaps">false</item>
<item name="android:textSize">18sp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="FlatButton" parent="Theme.AppCompat.Light">
<item name="android:textColor">@color/grey_dark</item>
<item name="colorControlHighlight">@color/white</item>
</style>
我看到两种可能性:
您的 Button 可能没有合适的父级。尝试:
<style name="buttonStartStyle" parent ="android:Widget.Button">
属性可能会在程序中发生变化。在应用样式之前尝试将所需的属性设置为 null。
这里的问题很可能与themes.xml中的父样式有关。
如果父主题是 Material Components 主题之一:
<style name="Theme.StyleDemo2" 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>
<!-- Customize your theme here. -->
</style>
这会覆盖您可能做出的任何样式更改,以便为按钮提供材质主题,并具有由主题中的 colorPrimary 项定义的色调颜色(在本例中它们也不是标准按钮,而是悄悄替换了一些时髦的子类)。要解决此问题,请将父主题更改为:
parent="Theme.AppCompat.Light"
这个更加可定制,您的按钮样式将会生效。
如果你使用Theme.MaterialComponents...,你应该使用materialButtonStyle,而不是buttonStyle