您好,我想创建进度条,应如上图所示。在这里,我创建了一个如下图所示的图像。
下面是我在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完全相同的进度条,如何实现?
最佳答案
自定义进度条需要定义进度条的背景和进度的属性。
在 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
关于android - 在android中创建矩形进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20064311/