【发布时间】: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