如何在 Android 中自定义进度条 [英] How to Customize a Progress Bar In Android

查看:38
本文介绍了如何在 Android 中自定义进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,我想在其中显示 ProgressBar,但我想替换默认的 Android ProgressBar.

I am working on an app in which I want to show a ProgressBar, but I want to replace the default Android ProgressBar.

那么如何自定义ProgressBar?

为此我需要一些图形和动画吗?

Do I need some graphics and animation for that?

我阅读了以下帖子,但无法使其正常工作:

I read the following post but could not get it to work:

Android 自定义进度条

推荐答案

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

Customizing a ProgressBar requires defining the attribute or properties for the background and progress of your progress bar.

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

Create an XML file named customprogressbar.xml in your res->drawable folder:

custom_progressbar.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>
</layer-list> 

现在你需要在customprogressbar.xml(drawable)

Now you need to set the progressDrawable property in customprogressbar.xml (drawable)

您可以在 XML 文件或活动中(在运行时)执行此操作.

You can do this in the XML file or in the Activity (at run time).

在您的 XML 中执行以下操作:

Do the following in your 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 drawable
    progressBar.setProgressDrawable(draw);

更正的 xml 布局

这篇关于如何在 Android 中自定义进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆