【问题标题】:Button Style does not work in Android Studio按钮样式在 Android Studio 中不起作用
【发布时间】:2020-12-30 16:15:35
【问题描述】:

我正在使用最新的 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>

【问题讨论】:

    标签: java android xml button


    【解决方案1】:

    我看到了两种可能:

    • 您的 Button 可能没有合适的父级。试试:

      &lt;style name="buttonStartStyle" parent ="android:Widget.Button"&gt;

    • 在您的程序中,属性可能会随之改变。在应用样式之前尝试将所需的属性设置为 null。

    【讨论】:

    • 您好,感谢您的快速回答,但两种解决方案均无效
    【解决方案2】:

    这里的问题很可能与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"
    

    这个更可定制,您的按钮样式将生效。

    【讨论】:

      猜你喜欢
      • 2017-10-28
      • 2015-09-01
      • 2023-03-26
      • 2015-07-05
      • 2023-01-09
      • 2018-07-18
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      相关资源
      最近更新 更多