android 设置进度条最大值和最小值时,无法更改进度条的进度

lkaoscv7  于 2023-01-19  发布在  Android
关注(0)|答案(2)|浏览(202)

如何改变进度条的进度颜色时,你已经设置了进度条的最大值和最小值。我有一个进度条,并试图设置最大值为15和最小值为-5。我想显示从-5到0的进度,并将进度的颜色更改为红色,并显示绿色为0到15的进度条。

<style name="CustomProgressBarReturnRange" parent="android:Widget.ProgressBar.Horizontal">
        <item name="android:indeterminateOnly">false</item>
        <item name="android:minHeight">4dp</item>
        <item name="android:maxHeight">4dp</item>
    </style>

   <ProgressBar
        android:id="@+id/returnProgress"
        style="@style/CustomProgressBarReturnRange"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="15"
        android:min="-5"
        android:progress="0" 
        tools:progress="20" />
9gm1akwq

9gm1akwq1#

试试这个
渐变可绘制

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/progress">
    <item android:id="@android:id/progress">
        <clip>
            <shape android:shape="rectangle">
                <gradient
                    android:centerColor="#0C8110"
                    android:centerX=".4"
                    android:endColor="#045C07"
                    android:startColor="#C13C3C"
                    android:type="linear" />
            </shape>
        </clip>
    </item>
</layer-list>

线性可绘制

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/progress">
<item android:id="@android:id/progress">
    <clip>
        <layer-list>
            <item android:width="80dp">
                <shape android:shape="rectangle" >
                    <size android:height="20dp" />
                    <solid android:color="#ff0000" />
                </shape>
            </item>
            <item android:start="81dp">
                <shape android:shape="rectangle" >
                    <size android:height="20dp" />
                    <solid android:color="#1AFF00" />
                </shape>
            </item>
        </layer-list>

    </clip>
</item>

你的进度条是这样的

<ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminate="false"
        android:max="15"
        android:min="-5"
        android:progress="10"
        android:progressDrawable="@drawable/your_drawable"
        style="?android:attr/progressBarStyleHorizontal"
        android:id="@+id/mProgressBar"/>
py49o6xq

py49o6xq2#

<style name="CustomProgressBarReturnRange" parent="android:Widget.ProgressBar.Horizontal">
        <item name="android:indeterminateOnly">false</item>
        <item name="android:progressDrawable">@drawable/custom_return_range_progress</item>
        <item name="android:minHeight">4dp</item>
        <item name="android:maxHeight">4dp</item>
    </style>

<ProgressBar
       android:id="@+id/returnProgress"
       style="@style/CustomProgressBarReturnRange"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:max="15"
       android:min="-5"
       android:progress="0" />

if (progressBar.progress <= 0) 
{
  progressBar.progressTintList = ColorStateList.valueOf(Color.RED)
} 
else if (progressBar.progress > 0)
{
  progressBar.progressTintList = ColorStateList.valueOf(Color.WHITE)
}

相关问题