为了账号安全,请及时绑定邮箱和手机立即绑定

如何在Android中自定义进度栏

如何在Android中自定义进度栏

慕沐林林 2019-07-22 15:41:37
如何在Android中自定义进度栏我正在开发一个应用程序,我想在其中显示一个ProgressBar,但我想替换默认的android。ProgressBar.那么如何自定义ProgressBar?我需要一些图形和动画吗?我读了下面的文章,但没能让它起作用:自定义进度栏Android
查看完整描述

1 回答

?
神不在的星期二

TA贡献1963条经验 获得超6个赞

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

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

自定义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>

现在,您需要设置progressDrawable财产customprogressbar.xml(可拖)

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

在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布局


查看完整回答
反对 回复 2019-07-22
  • 1 回答
  • 0 关注
  • 440 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信