obtainStyledAttributes

可以看到android 给自定义View提供了好几个构造函数。相对应的也有好几个obtainStyledAttributes重载方法

  • obtainStyledAttributes(@StyleableRes int[] attrs):从系统主题theme中获取attrs中的属性

  • obtainStyledAttributes(@StyleRes int resid, @StyleableRes int[] attrs) throws Resources.NotFoundException:从资源文件定义的style中读取属性,可能抛出运行时异常

  • obtainStyledAttributes(AttributeSet set, @StyleableRes int[] attrs):从layout设置的属性集中获取attrs中的属性,通常inflate布局就是使用的这个构造

  • obtainStyledAttributes(AttributeSet set, @StyleableRes int[] attrs, @AttrRes int defStyleAttr,@StyleRes int defStyleRes)

参数说明

  • @StyleableRes int[] attrs:id数组,告诉我们需要获取的属性

  • AttributeSet set:包含属性的集合,如android:layout_width=”match_parent”等,包含了两种来源
    一种是layout中直接指定,如android:src=”@mipmap/ic_launcher”
    一种是layout中的style引用,如 style=”@style/TextAppearance.AppCompat.Large”

  • @AttrRes int defStyleAttr:是theme 中的一个属性attr,是一个style引用。

ps:ViewPagerIndicator中style属性attr的定义;

 <declare-styleable name="ViewPagerIndicator">        <!-- Style of the circle indicator. -->        <attr name="vpiCirclePageIndicatorStyle" format="reference"/>        <!-- Style of the icon indicator's views. -->        <attr name="vpiIconPageIndicatorStyle" format="reference"/>        <!-- Style of the line indicator. -->        <attr name="vpiLinePageIndicatorStyle" format="reference"/>        <!-- Style of the title indicator. -->        <attr name="vpiTitlePageIndicatorStyle" format="reference"/>        <!-- Style of the tab indicator's tabs. -->        <attr name="vpiTabPageIndicatorStyle" format="reference"/>        <!-- Style of the underline indicator. -->        <attr name="vpiUnderlinePageIndicatorStyle" format="reference"/>    </declare-styleable>

CircleIndicator中的引用

public CirclePageIndicator(Context context, AttributeSet attrs) {        this(context, attrs, R.attr.vpiCirclePageIndicatorStyle);    }    public CirclePageIndicator(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        if (isInEditMode()) return;    }

注意:自定义控件中如果传入此参数,则会从 layout属性 > layout中style属性 > theme中item为此style的属性 的顺序来查找相应的属性值。
如果此参数传入0,则表示不向Theme中搜索默认值。

  • @StyleRes int defStyleRes || @StyleRes int resid:从资源文件中定义的某个样式中读取,
    仅在@AttrRes defStyleAttr = 0@AttrRes defStyleAttr != 0但Theme中没有为@AttrRes defStyleAttr属性赋值时起作用。

因此可以看到在CircleIndicator中是将此参数直接传入0的,因为它不起作用

//Retrieve styles attributesTypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CirclePageIndicator, defStyle, 0);

小范围上的属性会覆盖大范围的属性,其优先级为
AttributeSet(layout.xml){style.xml指定 优先于 android: 指定 } > defStyleAttr(@AttrRes主题可配置样式attr) > defStyleRes(@StyleRes独立样式style) > (主题中直接指定的样式item)

参考:深入理解Android 自定义attr Style styleable以及其应用

Android中自定义样式与View的构造函数中的第三个参数defStyle的意义

更多相关文章

  1. RadioButton和CheckBox自定义按钮图片的样式
  2. RelativeLayout布局,以及部分常用属性介绍scaleType/gravity/layo
  3. Android 利用Sharp样式设置文本框EditText圆角形状
  4. Android 属性系统 详解
  5. Android访问WCF服务(使用json实现参数传递)

随机推荐

  1. Android(安卓)UI学习 Linear Layout, Rel
  2. adobe air android INSTALL_FAILED_INVAL
  3. Android(安卓)Nine-patch(.9.png)小结
  4. raspberryPi2 移植Android系统
  5. eclipse 和 android studio 快捷键对比
  6. 利用adb安装和卸载android APK
  7. Android中文API(97)—— ContextMenu
  8. UiModeManager设置夜间模式和行车模式
  9. unity3d 保存(截图、拍照)图片 到Android(
  10. 几个Android常见wraning警告处理方法