英文:
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
Color used:
<color name="colorYellow">#E2DA85</color>
Pic 2 (Actual Behavior): When I set milk or any light shade color as the background to the parent layout
Color used:
<color name="colorMilk">#FDFDF5</color>
Here is my activity layout file:
<?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>
Please note
> android:background="@color/colorYellow"
And here is the custom style for progressbar named 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>
And here is the custom drawable file named progressbar_book that I made for the progressbar:
<?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>
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
Pic 2: Background as colorYellow
I used #7F1419 as progress color
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论