当父布局的背景色为浅色时,Android ProgressBar 的背景色会变为灰色。

3

尝试了很多解决方案,但都没有解决这个问题。

问题: 我在我的Android应用程序中有一个进度条,并且我已经应用了自定义样式,使其水平并更改了其背景和进度颜色。然而,当我将浅色设置为父布局的背景时,进度条的背景颜色会变成灰色阴影,而不是我在样式中指定的原始颜色。

图片1(期望行为):当我将深色设置为父布局的背景时

enter image description here

使用的颜色:

<color name="colorYellow">#E2DA85</color>

图片2(实际行为):当我将牛奶或任何浅色调用作父布局的背景时

enter image description here

所使用的颜色:

<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>

请注意:

android:background="@color/colorYellow"

这里是名为CustomProgressBar的自定义进度条样式:
<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>

当父布局具有浅色背景时,如何使进度条显示正确的背景颜色?

1个回答

2

看起来你的代码没有问题。你的colorCotton颜色可能有点灰色。由于你没有提到颜色代码,我用你的ColorMilk替换了它,它完美地工作。

图片1:背景和进度条渐变颜色都是ColorMilk

enter image description here

图片2:背景为黄色

enter image description here

我使用#7F1419作为进度条颜色


我已更新问题以添加cottonColor。我的观点是,为什么在更改背景时相同的颜色会产生不同的阴影。 - Muhammad Hanzilah
还有,感谢您尝试。在第一张图片中,由于您将布局和进度条的背景颜色使用了相同的颜色,因此您将无法看到区别。 - Muhammad Hanzilah
ColorCotton看起来是白色的,带有黄色的背景,但它实际上略微带有灰色。您可以检查颜色调色板。这就是为什么当您更改浅色背景时会感觉到这种变化的原因。 - manivanna perumal
是的,看起来问题出在颜色上,它在不同的背景下表现不同。点赞并接受! - Muhammad Hanzilah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接