【问题标题】:android drawables are colored automatically with color primary and it will not changeandroid drawables自动使用原色着色,并且不会改变
【发布时间】:2021-04-09 10:59:20
【问题描述】:

当我将任何 View 的背景更改为某个可绘制对象时,它会以原色着色(在我的情况下为 #FF6200EE),并且不会改变。

例如,当我更改按钮的背景时,背景颜色会自动设置为原色,即使设置了背景色也不会更改

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="@drawable/rectangle"
    android:backgroundTint="@color/white"/>

这里是 rectangle.XML 文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle">
<size android:width="40dp" android:height="40dp" />
<solid android:color="@color/white"/>
</shape>

编辑:这是整个主要的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/Theme.TourGuide.AppBarOverlay">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary" />
</com.google.android.material.appbar.AppBarLayout>

<androidx.viewpager.widget.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="@drawable/rectangle"
    style="@style/Widget.AppCompat.Button.Colored"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

这里是themes.xml 文件:

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.TourGuide" 
parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- 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>

<style name="Theme.TourGuide.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="Theme.TourGuide.AppBarOverlay" 
parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="Theme.TourGuide.PopupOverlay" 
parent="ThemeOverlay.AppCompat.Light" />
</resources>

【问题讨论】:

  • 我刚刚将您的 rectangle.xml 添加到我的项目中并使用按钮进行了尝试,一切都很好。除非您的 XML 中没有结束标记 。但这不是问题。请检查您是否有一些与原色重叠的布局,或者您是否正在以编程方式更改它,或者如果您添加了任何可以做到这一点的属性,甚至可以检查您的主题。
  • @SlothCoding 谢谢,我只是忘记复制结束标签,谢谢您的建议,我怀疑这是第三个,当我检查 values.XML 中的样式时,我真的很困惑,所以任何帮助都会受到欢迎
  • 你能编辑你的问题并添加你的styles.xml和你创建按钮的整个XML。
  • @SlothCoding 在我的项目中没有生成styles.xml 文件,尽管我在themes.XML 中找到了样式

标签: android colors background-color drawable


【解决方案1】:

首先,您使用的是 MaterialComponents 主题,我建议您也使用 Material Components。代替 Button,您可以使用:

com.google.android.material.button.MaterialButton

您可以在此处找到有关 MaterialComponents 和 Material Themes 的更多信息:https://material.io/components?platform=android

要更改 Button 或 MaterialButton 的颜色,您需要使用:

app:backgroundTint="@color/somecolor"

代替:

android:backgroundTint="@color/somecolor"

让我知道它是否有效!

【讨论】:

  • 非常感谢它的工作,最后一件事,我想知道你为什么推荐 MaterialButton 而不是 Button
  • 我认为它具有更多的属性,并且与 MaterialComponents 主题更兼容。检查我给你的链接,那里有所有东西,它是关于如何创建应用程序、采用哪些维度以及其他内容的“标准”。
  • 好的,我会检查一下,非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 2019-10-05
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多