我们在自定义View 时,一般都会用到

TypedArray obtainStyledAttributes(

                AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes);  对于它的前面2个参数可能大家都知道, 对于后面两个参数一直不明白怎么用。

于是今天查了下。

           context obtainStyledAttributes 最终会调到Resources.Theme.obtainStyledAttributes(AttributeSet set,
                int[] attrs, int defStyleAttr, int defStyleRes) 

再看一下参数解释:

         * @param set The base set of attribute values.  May be null.
         * @param attrs The desired attributes to be retrieved.
         * @param defStyleAttr An attribute in the current theme that contains a
         *                     reference to a style resource that supplies
         *                     defaults values for the TypedArray.  Can be
         *                     0 to not look for defaults.
         * @param defStyleRes A resource identifier of a style resource that
         *                    supplies default values for the TypedArray,
         *                    used only if defStyleAttr is 0 or can not be found
         *                    in the theme.  Can be 0 to not look for defaults.


defStyleAttr 指向当前theme 某个item 描述的style 该style指定了一些默认值为这个TypedArray

defStyleRes  当defStyleAttr 找不到或者为0, 可以直接指定某个style


不是很理解 ,搜了一下源码:

发现用的地方 不多。如CalendarView

TypedArray attributesArray = context.obtainStyledAttributes(attrs, R.styleable.CalendarView,
                R.attr.calendarViewStyle, 0);

CalendarView  在 attrs.xml 中 定义如下:

    
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
   


默认 defStyleAttr 为 calendarViewStyle 

也定义在attrs.xml

       
       

再看看在theme.xml 中 它指向:

       

更多相关文章

  1. android自定义注解的运用
  2. android标题栏中添加返回按钮
  3. Android(安卓)ListView的OnItemClickListener()参数详解
  4. Android(安卓)popupWindow 用法
  5. android 实现男女按钮选择
  6. Android(安卓)模糊搜索rawquery bind or column index out of ra
  7. Android中自定义ScrollView代码实例
  8. Android笔记(1)adb的使用
  9. Android摄像头采集Demo

随机推荐

  1. Android(一)开发环境的搭建
  2. Andriod使用webview控件往APP里内嵌网页
  3. android中判断横屏或者竖屏并改变背景
  4. Android调用系统相机拍照并保存到指定位
  5. Android: JNI本地函数控制Java端代码
  6. android上,实现直接在屏幕上显示点击位置,
  7. SDK和Android(安卓)Studio的下载安装
  8. android edittext 去边框
  9. windows 系统Android模拟器联网设置
  10. Android 高德地图在清除Marker的时候会把