Android ProgressBar父布局浅色背景时背景色异常变灰求助
问题:Android水平ProgressBar在浅色父布局背景下背景色异常
问题现象
为水平ProgressBar自定义了背景和进度颜色,父布局为深色(如#E2DA85)时显示正常,但父布局改为浅色(如#FDFDF5)后,ProgressBar的背景色变成灰色,而非指定的#F2F6EE。
相关代码
布局文件
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:background="@color/colorYellow" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ProgressBar android:id="@+id/progressBook" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginHorizontal="20dp" android:layout_marginTop="100dp" android:progress="75" style="@style/CustomProgressBar" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
自定义ProgressBar样式
<style name="CustomProgressBar" parent="android:Widget.ProgressBar.Horizontal"> <item name="android:indeterminateOnly">false</item> <item name="android:progressDrawable">@drawable/progressbar_book</item> <item name="android:minHeight">10dip</item> <item name="android:maxHeight">20dip</item> </style>
自定义Drawable(progressbar_book.xml)
<?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape> <corners android:radius="10dp" /> <gradient android:startColor="@color/colorCotton" android:centerColor="@color/colorCotton" android:centerY="0.50" android:endColor="@color/colorCotton" android:angle="270" /> </shape> </item> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="10dp" /> <gradient android:startColor="@color/colorViolet" android:endColor="@color/colorViolet" android:angle="90" /> </shape> </clip> </item> </layer-list>
颜色定义
<color name="colorCotton">#F2F6EE</color> <color name="colorYellow">#E2DA85</color> <color name="colorMilk">#FDFDF5</color>
解决方案
该问题多由系统自动主题适配(深色/浅色模式切换)或ProgressBar默认tint属性干扰导致,可尝试以下解决方法:
方法1:替换Gradient为Solid(优先推荐)
既然背景是单一纯色,无需使用渐变,改用solid可避免系统潜在的着色干扰,同时提升性能:
<?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape> <corners android:radius="10dp" /> <solid android:color="@color/colorCotton" /> </shape> </item> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="10dp" /> <solid android:color="@color/colorViolet" /> </shape> </clip> </item> </layer-list>
方法2:禁用ProgressBar的默认Tint属性
在ProgressBar布局中添加属性,强制系统不对Drawable应用自动着色:
<ProgressBar android:id="@+id/progressBook" ... android:tint="@null" android:progressTint="@null" android:progressBackgroundTint="@null" ... />
方法3:在自定义样式中明确禁用主题着色
在CustomProgressBar样式中添加以下属性,锁定自定义Drawable的原始颜色:
<style name="CustomProgressBar" parent="android:Widget.ProgressBar.Horizontal"> ... <item name="android:progressTintMode">src_in</item> <item name="android:progressBackgroundTintMode">src_in</item> <item name="android:progressTint">@null</item> <item name="android:progressBackgroundTint">@null</item> </style>
方法4:关闭自动暗色主题适配
若应用开启了系统自动深色模式适配,可在ProgressBar中禁用该特性:
<ProgressBar ... android:forceDarkAllowed="false" ... />
内容的提问来源于stack exchange,提问作者Muhammad Hanzilah
相关产品推荐
相关产品推荐

