在android中创建矩形进度条

时间:2013-11-19 06:01:35

标签: android android-progressbar

enter image description here

您好,我想创建一个应该如上图所示的进度条。在这里,我创建了一个如下图像。

enter image description here

下面是我在res,

下的drawable文件夹中创建的xml文件

progress.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 android:shape="rectangle">
            <solid android:color="@color/white"/>
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape android:shape="rectangle">
                <solid android:color="@color/lightgreen"/>
            </shape>
        </clip>
    </item>

</layer-list>

但是当进度继续时,我想要与图像1完全相同的进度条,如何实现它?

2 个答案:

答案 0 :(得分:3)

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

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

<强> customprogressbar.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>

现在您需要设置将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" />

在运行时执行以下操作

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

有关详细信息,请查看HERE

同时检查How to Customize ProgressBar in Android?

答案 1 :(得分:0)

您需要更改xml以提及开始颜色,结束颜色和居中。

正确提及颜色值:start-color:left,end-color:right和center。