当前位置: 移动技术网 > IT编程>移动开发>Android > Android编程自定义进度条颜色的方法详解

Android编程自定义进度条颜色的方法详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

郭敬明小说txt下载,黄冈信息网,明日菜美容部员

本文实例讲述了android编程自定义进度条颜色的方法。分享给大家供大家参考,具体如下:

先看效果图:

老是提些各种需求问题,我觉得系统默认的颜色挺好的,但是pk不过,谁叫我们不是需求人员呢,改吧!

这个没法了只能看源码了,还好下载了源码, sources\base\core\res\res\ 下应有尽有,修改进度条颜色只能找progress ,因为是改变样式,首先找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"
 style="@style/progressbar_mini"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:progress="50" />

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网