【问题标题】:Android studio can't override default themeAndroid Studio 无法覆盖默认主题
【发布时间】:2021-09-24 18:07:46
【问题描述】:

我正在尝试将主题分配给具有自定义背景的按钮。它看起来就像这样:

<style name="button">
    <item name="android:background">@drawable/buttonbackground</item>
</style>‌

<Button android:theme="@style/button"/>

按钮保持其原始背景,但对于 TextView,例如,它可以工作。

<TextView android:theme="@style/button"/>

问题出在哪里?

【问题讨论】:

  • 您可以进一步简化您的问题并尝试&lt;Button android:background="@drawable/buttonbackground"/&gt;。如果这确实有效,那么问题与样式有关。如果这不起作用,那么问题出在属性上(因为您使用了错误的属性)
  • 您使用的是 Material Theme 还是 App Compat Theme?
  • @Blundell 这适用于我当前使用的应用程序主题,但是当我使用默认主题(Theme.MaterialComponents.DayNight.DarkActionBar)时,它也不起作用。问题肯定出在样式上,因为当我删除应用程序主题时,按钮会起作用。但显然该应用程序看起来像垃圾。
  • @Pinakin 我正在使用 Theme.AppCompat.Light.NoActionBar

标签: android android-studio android-layout


【解决方案1】:

更改您的应用主题:

    <style name="Theme" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge">
        .....
    </style>

在 AndroidManifest.xml 中使用@style/Theme:

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme"
        >

【讨论】:

    【解决方案2】:

    嗯,这是一个非常简单的解决方案,只需使用style 而不是android:theme。我只是不知道它的存在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      • 2017-08-06
      • 2021-03-26
      • 1970-01-01
      • 2015-02-25
      相关资源
      最近更新 更多