ProgressBar这个类在/froyo/frameworks/base/core/java/android/widget/ProgressBar.java

当你去new一个ProgressBar的时候需要传进去参数,我们从这里看一下它执行的流程。

本篇的主要意图还是告诉你如何修改style去改变样式,如果你想diy自己的ProgressBar,相信对作移植的朋友有所帮助。

ProgressBar.java

    public ProgressBar(Context context, AttributeSet attrs) {        this(context, attrs, com.android.internal.R.attr.progressBarStyle);         //如果你只是给出ProgressBar的参数集,那么就会去找默认的那个progressBar的style;         //我们就以默认的style作为示例,从这里去调下面的 构造方法      }    public ProgressBar(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        mUiThreadId = Thread.currentThread().getId();        //这里不多说,我们需要UI线程才能画图         initProgressBar();        TypedArray a =            context.obtainStyledAttributes(attrs, R.styleable.ProgressBar, defStyle, 0);        //这里才是关键,通过传过来的defstyle的名字 com.android.internal.R.attr.progressBarStyle 找到themes.xml中的item 

//通过这个item的name去styles.xml中找相应的style,具体过程如下:


//R.styleable.ProgressBar是声明参数列表和参数类型的,用于跟前面的attrs匹配,封装成TypedArray

//这个过程在 /froyo/frameworks/base/core/java/android/content/Context.java

// /froyo/frameworks/base/core/java/android/content/res/Resource.java

//里面可以找到。

//下面就是初始化一些参数了,不必细述。

...
}
attrs.xml 声明了ProgressBar这个styleable参数列表其他的widget下的公共控件也是如此。

R.styleable.ProgressBar

  <declare-styleable name="ProgressBar">        <!-- Defines the maximum value the progress can take. -->        <attr name="max" format="integer" />        <!-- Defines the default progress value, between 0 and max. -->        <attr name="progress" format="integer" />        <!-- Defines the secondary progress value, between 0 and max. This progress is drawn between             the primary progress and the background.  It can be ideal for media scenarios such as             showing the buffering progress while the default progress shows the play progress. -->        <attr name="secondaryProgress" format="integer" />        <!-- Allows to enable the indeterminate mode. In this mode the progress         bar plays an infinite looping animation. -->        <attr name="indeterminate" format="boolean" />        <!-- Restricts to ONLY indeterminate mode (state-keeping progress mode will not work). -->        <attr name="indeterminateOnly" format="boolean" />        <!-- Drawable used for the indeterminate mode. -->        <attr name="indeterminateDrawable" format="reference" />        <!-- Drawable used for the progress mode. -->        <attr name="progressDrawable" format="reference" />        <!-- Duration of the indeterminate animation. -->        <attr name="indeterminateDuration" format="integer" min="1" />        <!-- Defines how the indeterminate mode should behave when the progress        reaches max. -->        <attr name="indeterminateBehavior">            <!-- Progress starts over from 0. -->            <enum name="repeat" value="1" />            <!-- Progress keeps the current value and goes back to 0. -->            <enum name="cycle" value="2" />        </attr>        <attr name="minWidth" format="dimension" />        <attr name="maxWidth" />        <attr name="minHeight" format="dimension" />        <attr name="maxHeight" />        <attr name="interpolator" format="reference" />    </declare-styleable>

themes.xml 定义了一些索引式的item,通过这些item可以找到相应的style,通过下面的红色部分可以去styles.xml中找对应的style

  <style>        <item name="progressBarStyle">@android:style/Widget.ProgressBar </item>     <style>styles.xml 定义了各种style的内容,包含图片,animation,尺寸等,通过下面红字部分可以找到图片     <style name="Widget.ProgressBar">        <item name="android:indeterminateOnly">true</item>        <item name="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>

drawable/ progress_medium_white.xml

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@drawable/spinner_white_48"    android:pivotX="50%"    android:pivotY="50%"    android:framesCount="12"    android:frameDuration="100" />
 
1. 引用系统资源android:id="@android:id/text1"android:id和@id一样是引用已有的id,当然也包括系统(frameworks)的id,而@+id是新增加一个idandroid:textColor="@android:color/opaque_red" 指定package: android3. 引用主题属性另外一种资源值允许你引用当前主题中的属性的值。这个属性值只能在样式资源和XML属性中使用;它允许你通过将它们改变为当前主题提供的标准变化来改变UI元素的外观,而不是提供具体的值。   android:textColor="?android:textDisabledColor"     注意,这和资源引用非常类似,除了我们使用一个"?"前缀代替了"@"。当你使用这个标记时,你就提供了属性资源的名称,它将会在主题中被查找--因为资源工具知道需要的属性资源,所以你不需要显示声明这个类型(如果声明,其形式就是?android:attr/android:textDisabledColor)。除了使用这个资源的标识符来查询主题中的值代替原始的资源,其命名语法和"@"形式一致:?[namespace:]type/name,这里类型可选。  

引用: http://blog.csdn.net/gaogaf/archive/2010/12/20/6086846.aspx

更多相关文章

  1. 浅谈android的am命令
  2. Android(安卓)PopupWindow 的显示位置
  3. Android夜间模式实现
  4. TextView常用属性介绍
  5. Android(安卓)task和affinity讲解
  6. Android(安卓)5.0+ 高级动画开发系列 矢量图动画
  7. ListView小知识整理:滑动背景、Item间隙等
  8. android学习笔记16:带提示的输入框
  9. android之Intent基本使用

随机推荐

  1. android 工具类 数据库管理
  2. Android中字体颜色大全-146种(完整版)
  3. Android(安卓)Studio入门小例子
  4. android按键模拟测试
  5. android 里面的测量单位
  6. Android录制视频,可限制时长,限大小
  7. android四种不同的事件实现
  8. Utilize Avahi Libraries in Android(安
  9. Android:入门级Android开发者应该知道的
  10. Unity3d - Android(安卓)Build : Android