Android ProgressBar的背景颜色在父布局的背景为浅色时变为灰色。

huangapple go评论65阅读模式
英文:

Android ProgressBar background color gets gray when parent layout's background is light

问题

尝试了许多解决方案,但对于这个问题没有奏效。

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

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

颜色使用:

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

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

颜色使用:

<color name="colorMilk">#FDFDF5</color>

请注意:

android:background="@color/colorYellow"

这是我的活动布局文件:

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

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

我如何让进度条在父布局具有浅色背景时显示正确的背景颜色?

英文:

Tried many solutions but didn't work for this problem

Problem:
I have a progress bar in my Android app, and I've applied a custom style to it that makes it horizontal and changes its background and progress color. However, when I set a light color as the background of the parent layout, the progress bar's background color gets a gray shade instead of the original color I specified in the style.

Pic 1 (Expected Behavior): When I set a dark color as the background to the parent layout

Android ProgressBar的背景颜色在父布局的背景为浅色时变为灰色。

Color used:

&lt;color name=&quot;colorYellow&quot;&gt;#E2DA85&lt;/color&gt;

Pic 2 (Actual Behavior): When I set milk or any light shade color as the background to the parent layout

Android ProgressBar的背景颜色在父布局的背景为浅色时变为灰色。

Color used:

&lt;color name=&quot;colorMilk&quot;&gt;#FDFDF5&lt;/color&gt;

Here is my activity layout file:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:background=&quot;@color/colorYellow&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

    &lt;ProgressBar
        android:id=&quot;@+id/progressBook&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginHorizontal=&quot;20dp&quot;
        android:layout_marginTop=&quot;100dp&quot;
        android:progress=&quot;75&quot;
        style=&quot;@style/CustomProgressBar&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

Please note

> android:background="@color/colorYellow"

And here is the custom style for progressbar named CustomProgressBar:

&lt;style name=&quot;CustomProgressBar&quot; parent=&quot;android:Widget.ProgressBar.Horizontal&quot;&gt;
    &lt;item name=&quot;android:indeterminateOnly&quot;&gt;false&lt;/item&gt;
    &lt;item name=&quot;android:progressDrawable&quot;&gt;@drawable/progressbar_book&lt;/item&gt;
    &lt;item name=&quot;android:minHeight&quot;&gt;10dip&lt;/item&gt;
    &lt;item name=&quot;android:maxHeight&quot;&gt;20dip&lt;/item&gt;
&lt;/style&gt;

And here is the custom drawable file named progressbar_book that I made for the progressbar:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;layer-list xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
    &lt;item android:id=&quot;@android:id/background&quot;&gt;
        &lt;shape&gt;
            &lt;corners android:radius=&quot;10dp&quot; /&gt;
            &lt;gradient
                android:startColor=&quot;@color/colorCotton&quot;
                android:centerColor=&quot;@color/colorCotton&quot;
                android:centerY=&quot;0.50&quot;
                android:endColor=&quot;@color/colorCotton&quot;
                android:angle=&quot;270&quot; /&gt;
        &lt;/shape&gt;
    &lt;/item&gt;
    &lt;item android:id=&quot;@android:id/progress&quot;&gt;
        &lt;clip&gt;
            &lt;shape&gt;
                &lt;corners android:radius=&quot;10dp&quot; /&gt;
                &lt;gradient
                    android:startColor=&quot;@color/colorViolet&quot;
                    android:endColor=&quot;@color/colorViolet&quot;
                    android:angle=&quot;90&quot; /&gt;
            &lt;/shape&gt;
        &lt;/clip&gt;
    &lt;/item&gt;
&lt;/layer-list&gt;

colorCotton:

&lt;color name=&quot;colorCotton&quot;&gt;#F2F6EE&lt;/color&gt;

How can I get the progressbar to show the correct background color even when the parent layout has a light color background?

答案1

得分: 2

It seems there is no issue with your code. Your colorCotton colour may have a little bit grey. since you didn't mention the colour code I replaced it with your ColorMilk and its works perfectly fine.

Pic 1: Both background and Progressbar gradient colour as ColorMilk

Pic 2: Background as colorYellow

I used #7F1419 as progress color

英文:

It seems there is no issue with your code. Your colorCotton colour may have a little bit grey. since you didn't mention the colour code I replaced it with your ColorMilk and its works perfectly fine.

Pic 1: Both background and Progressbar gradient colour as ColorMilk

Android ProgressBar的背景颜色在父布局的背景为浅色时变为灰色。

Pic 2: Background as colorYellow

Android ProgressBar的背景颜色在父布局的背景为浅色时变为灰色。

I used #7F1419 as progress color

huangapple
  • 本文由 发表于 2023年5月14日 20:16:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定