尝试了很多解决方案,但都没有解决这个问题。
问题: 我在我的Android应用程序中有一个进度条,并且我已经应用了自定义样式,使其水平并更改了其背景和进度颜色。然而,当我将浅色设置为父布局的背景时,进度条的背景颜色会变成灰色阴影,而不是我在样式中指定的原始颜色。
图片1(期望行为):当我将深色设置为父布局的背景时
使用的颜色:
<color name="colorYellow">#E2DA85</color>
图片2(实际行为):当我将牛奶或任何浅色调用作父布局的背景时
所使用的颜色:
<color name="colorMilk">#FDFDF5</color>
这是我的活动布局文件:
<?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>
请注意:
这里是名为CustomProgressBar的自定义进度条样式:android:background="@color/colorYellow"
<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>
这是我为进度条制作的自定义可绘制文件,名为progressbar_book:
<?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>
colorCotton:
<color name="colorCotton">#F2F6EE</color>
当父布局具有浅色背景时,如何使进度条显示正确的背景颜色?