android - 在android中创建矩形进度条

标签 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完全相同的进度条,如何实现?

最佳答案

自定义进度条需要定义进度条的背景和进度的属性。

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?

关于android - 在android中创建矩形进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20064311/

相关文章:

android - 带有 Thumb Android 的自定义 ProgressBar

java - 读取位图文件返回 null

Android:除了屏幕打开和关闭之外,是否有任何 Intent 监听 CPU "sleep"和 "wake Up"?

java - 如何关闭突出通知?

android - LinearLayout 水平在真实设备上显示不正确

android - 更新 ListView 中的进度条

安卓定期同步

java - 在加载动画期间使背景元素变暗/变暗 - Android

android - 关于 Android 进度对话框。避免?

android - 如何在 android 的小部件中以编程方式设置辅助进度?