自定义进度条Android

Ses*_*nay 9 customization android progress-bar

我想在android中使用这种类型的进度条.我试过很多水平进度条.它们看起来都像是具有不同颜色的默认进度条.不知道如何使用这种类型:在此输入图像描述

Kum*_*bek 11

您需要创建自己的自定义进度条.它不像使用许多水平条那么简单.


小智 5

自定义进度条需要为进度条的背景和进度定义一个或多个属性。

在 res-> drawable 文件夹中创建一个名为 customprogressbar.xml 的 .xml 文件

自定义进度条.xml

   <layer-list xmlns:android="http://schemas.android.com/apk/res/android">

        <!-- Define the background properties like color etc -->
    <item android:id="@android:id/background">
    <shape>
        <gradient
                android:startColor="#000001"
                android:centerColor="#0b131e"
                android:centerY="1.0"
                android:endColor="#0d1522"
                android:angle="270"
        />
    </shape>
   </item>

  <!-- Define the progress properties like start color, end color etc -->
  <item android:id="@android:id/progress">
    <clip>
        <shape>
            <gradient
                android:startColor="#007A00"
                android:centerColor="#007A00"
                android:centerY="1.0"
                android:endColor="#06101d"
                android:angle="270"
            />
        </shape>
    </clip>
</item>
Run Code Online (Sandbox Code Playgroud)

现在您需要设置将 progressDrawable 属性设置为 customprogressbar.xml(drawable)

您可以在 xml 文件或 Activity(在运行时)中执行此操作

在您的 xml 中执行以下操作

   <ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:progressDrawable="@drawable/custom_progressbar"         
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)

在运行时执行以下操作

      // Get the Drawable custom_progressbar                     
                              Drawable draw= res.getDrawable(R.drawable.custom_progressbar);
                              // set the drawable as progress drawavle

                              progressBar.setProgressDrawable(draw);
Run Code Online (Sandbox Code Playgroud)