Android-Horizo​​ntal Gradient进度条显示完整进度

时间:2018-06-16 17:41:03

标签: android android-layout android-studio progress-bar

带有进度绘制的进度条未显示正确的进度。它显示完整进度而不是设置进度值。

进度条xml

 <ProgressBar
       style="@style/Widget.AppCompat.ProgressBar.Horizontal"
       android:layout_centerVertical="true"
       android:layout_toRightOf="@id/rewardScore"
       android:layout_width="match_parent"
       android:id="@+id/userProgress"
       android:max="100"
       android:background="@drawable/progress_white_bg"
       android:layout_marginLeft="10dp"
       android:layout_height="10dp"
       android:progress="40"
       android:progressDrawable="@drawable/gradient_progress"
    />

gradient_drawable.xml

<?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="100dp"/>
        <gradient
            android:startColor="@color/white"
            android:endColor=  "@color/white"
            />
        </shape>
     </item>

     <item android:id="@android:id/progress">
       <shape>
         <corners android:radius="100dp"/>

        <gradient
            android:startColor="@color/progress_gradient_one"
            android:endColor="@color/progress_gradient_two"
            />
       </shape>
    </item>
</layer-list>

2 个答案:

答案 0 :(得分:2)

问题在gradient_progress drawable中。在渐变中设置此属性  第二项的标签:android:useLevel =&#34; true&#34;:

<?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="1dp" />
        <gradient
            android:endColor="#fff"
            android:startColor="#fff" />
    </shape>
</item>

<item android:id="@android:id/progress">
    <shape>
        <corners android:radius="1dp" />
        <gradient
            android:endColor="#6aae33"
            android:startColor="#e61313"
            android:useLevel="true" />
    </shape>
</item>

答案 1 :(得分:0)

诀窍是在进度项目中添加<clip>标签

<?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="100dp"/>
        <gradient
            android:startColor="@color/white"
            android:endColor=  "@color/white"
            />
        </shape>
     </item>

     <item android:id="@android:id/progress">
       <clip>
         <shape>
           <corners android:radius="100dp"/>

           <gradient
               android:startColor="@color/progress_gradient_one"
               android:endColor="@color/progress_gradient_two"
            />
         </shape>
       </clip>
    </item>
</layer-list>