[英]Button Style does not work in Android Studio
T'm using the newest Android Studio Version to code my app and I can't change the button style.我正在使用最新的 Android Studio 版本来编写我的应用程序,但我无法更改按钮样式。 It seems like when generating a new project, it already has some default styles for buttons which I can't seem to be able to override.
似乎在生成一个新项目时,它已经有一些默认的 styles 用于我似乎无法覆盖的按钮。
Everytime I place just a plane button it already has some background color ("colorPrimary") and if I'm trying to apply a style, it only changes the text of the button, but not the shape or the color.每次我只放置一个平面按钮时,它已经有一些背景颜色(“colorPrimary”),如果我尝试应用一种样式,它只会更改按钮的文本,但不会更改形状或颜色。
This is my style:这是我的风格:
<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>
and my button_style1.xml:和我的 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>
The button already looks like this when I place it right from the pallete:当我将它从托盘中放置时,该按钮已经看起来像这样:
My xml code for the button:我的按钮 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" />
This is how the button looks with the style applied:这是应用样式后按钮的外观:
Here is my themes.xml:这是我的主题。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>
I see two possibilites:我看到两种可能性:
You might not have the right parent for your Button.您的 Button 可能没有合适的父级。 Try:
尝试:
<style name="buttonStartStyle" parent ="android:Widget.Button">
attributes might be changed along the way in your program.属性可能会在您的程序中沿途更改。 Try setting the needed attributes to null before applying the style.
在应用样式之前,尝试将所需的属性设置为 null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.