try 了许多解决方案,但对此问题都不起作用

Problem:个 我在我的Android应用程序中有一个进度条,我对它应用了一个自定义样式,使其成为水平的,并改变其背景和进度 colored颜色 .但是,当我将浅色设置为父布局的背景时,进度条的背景 colored颜色 变为灰色,而不是我在样式中指定的原始 colored颜色 .

当我将深色设置为父布局的背景时

enter image description here

Color used:个个

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

当我将牛奶或任何浅阴影 colored颜色 设置为父布局的背景时

enter image description here

Color used:个个

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

这是我的activity layout%文件:

<?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/COLOR黄色"
    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/COLOR黄色"

下面是名为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>

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

推荐答案

您的代码似乎没有问题.你的colorCotton色可能有点灰.因为你没有提到 colored颜色 代码,所以我把它换成了你的ColorMilk,它工作得很好.

图1:背景和进度条渐变 colored颜色 均为ColorMilk

enter image description here

图2:背景为colorYellow

enter image description here

我用#7F1419作为进度色

Android相关问答推荐

修改参数应该应用于哪些子元素?

如何使用视图模型触发可变状态?

Kotlin DSL:为什么我可以从Play Store获取发布版本的日志(log)?

如何在初始合成期间在可组合函数中调用/获取远程API中的数据[防止无限重组]

在内部创建匿名对象的繁忙循环调用函数会产生开销吗?

找不到类MultipartBody;的序列化程序

在更改MuableState的值后,Android API 26(Oreo)上的Composable不能重新组合

Jetpack Compose:如何将文本放置在行的右侧?

如何使用滑行加载媒体的专辑封面?

观察软键盘可见性,打开/关闭 Jetpack Compose

Jetpack compose 为网络检索视频帧导致延迟

如何处理 com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: For input string: "T1V 4Y8" Kotlin

如何在 Jetpack Compose 中更改 ModalNavigationDrawer 的抽屉容器 colored颜色 ?

在 Kotlin 客户端应用程序中发送 FCM 推送通知 - Firebase 云消息传递

android 13 版本是否会影响 android 12 目标应用程序

在 jetpack compose 中使用 viewmodel 的最佳实践

如何将房间数据库导出到 .CSV

操作系统会终止已启动的服务并调用Service.onDestroy吗?

如何获取 Material Design 3 的底部导航栏高度?

lambda 函数中的类型不匹配 - Kotlin