Android中怎么自定义进度条颜色

发布时间:2021-06-26 17:19:29 作者:Leah
来源:亿速云 阅读:228

今天就跟大家聊聊有关Android中怎么自定义进度条颜色,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

styles.xml

找到xml后,进去找到:

<style name="Widget.ProgressBar">
 <item name="android:indeterminateOnly">true</item>
 <itemname="android:indeterminateDrawable">@android:drawable/progress_medium_white</item>
 <item name="android:indeterminateBehavior">repeat</item>
 <item name="android:indeterminateDuration">3500</item>
 <item name="android:minWidth">48dip</item>
 <item name="android:maxWidth">48dip</item>
 <item name="android:minHeight">48dip</item>
 <item name="android:maxHeight">48dip</item>
</style>

这是那个默认转圈的,但今天我们不修改这个,我们是要改变水平进度条颜色,所以找到:

<style name="Widget.ProgressBar.Horizontal">
 <item name="android:indeterminateOnly">false</item>
 <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
 <itemname="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
 <item name="android:minHeight">20dip</item>
 <item name="android:maxHeight">20dip</item>
</style>

你看系统一步一步关联的,扩展性很性,低耦合,所以我们现在只要改变进度条是怎么样画出来的就行了 ,但是负责画进度条的是 <item name="android:progressDrawable">  所以我们可以找到"drawable下的 progress_horizontal 文件,改变他就可以改变进度条颜色。

<?xml version="1.0" encoding="utf-8"?>
<!--
 Copyright (C) 2008 The Android Open Source Project
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
 <item android:id="@android:id/background">
 <shape>
  <corners android:radius="5dip" />
  <gradient
  android:angle="270"
  android:centerColor="#ff5a5d5a"
  android:centerY="0.75"
  android:endColor="#ff747674"
  android:startColor="#ff9d9e9d" />
 </shape>
 </item>
 <item android:id="@android:id/secondaryProgress">
 <clip>
  <shape>
  <corners android:radius="5dip" />
  <gradient
   android:angle="270"
   android:centerColor="#80ffb600"
   android:centerY="0.75"
   android:endColor="#a0ffcb00"
   android:startColor="#80ffd300" />
  </shape>
 </clip>
 </item>
 <item android:id="@android:id/progress">
 <clip>
  <shape>
  <corners android:radius="5dip" />
  <gradient
   android:angle="270"
   android:centerColor="#ffffb600"
   android:centerY="0.75"
   android:endColor="#ffffcb00"
   android:startColor="#ffffd300" />
  </shape>
 </clip>
 </item>
</layer-list>

看到没有,这是系统的进度条画出的布局条件

android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:endColor="#ff747674"

我们只要改变这个色值就能改变他的颜色,主要改变的是<item android:id="@android:id/progress">下的色值就行了。

说了这么多,到底怎么做呢,很简单:

在我们的项目下新建一个 style.xml 文件

创建一个style 标签,集成系统默认样式,然后自定义一个新的progressDrawable 文件,随后面在layout 中的progress 中引用这个文件就行。

<style name="ProgressBar_Mini" parent="@android:style/Widget.ProgressBar.Horizontal">
 <item name="android:maxHeight">50dip</item>
 <item name="android:minHeight">8dip</item>
 <item name="android:indeterminateOnly">false</item>
 <itemname="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
 <item name="android:progressDrawable">@drawable/progressbar_mini</item>
</style>

下面是我的 progressbar_mini 文件,改变了下android:endColor="#F5F5F5" android:startColor="#BEBEBE" 的色值

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
 <item android:id="@android:id/background">
 <shape>
  <corners android:radius="5dip" />
  <gradient
  android:angle="270"
  android:centerY="0.75"
  android:endColor="#F5F5F5"
  android:startColor="#BEBEBE" />
 </shape>
 </item>
 <item android:id="@android:id/secondaryProgress">
 <clip>
  <shape>
  <corners android:radius="0dip" />
  <gradient
   android:angle="270"
   android:centerY="0.75"
   android:endColor="#165CBC"
   android:startColor="#85B0E9" />
  </shape>
 </clip>
 </item>
 <item android:id="@android:id/progress">
 <clip>
  <shape>
  <corners android:radius="5dip" />
  <gradient
   android:angle="270"
   android:centerY="0.75"
   android:endColor="#165CBC"
   android:startColor="#85B0E9" />
  </shape>
 </clip>
 </item>
</layer-list>

最后引用其就可以了。

<ProgressBar
 android:id="@+id/progress"
 
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:progress="50" />

看完上述内容,你们对Android中怎么自定义进度条颜色有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. Android中怎么对图片的颜色进行处理
  2. Android中怎么利用ProgressBar实现颜色渐变

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android

上一篇:Android中怎么监听软键盘状态

下一篇:Android中MeasureSpec的作用是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》