美文网首页
obtainStyledAttributes理解

obtainStyledAttributes理解

作者: proud2008 | 来源:发表于2017-08-11 16:10 被阅读611次

参考:http://www.jianshu.com/p/61b79e7f88fc
在使用

cn.finalteam.rxgalleryfinal 
com.shehabic.droppy

发现样式可在style中配置,
定义如下

/ * @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.
* /
/ * 
* @param attrs 要解析的那些属性值
* @param set layout的布局配置的
* @param defStyleAttr 通过attr设置
* @param defStyleRes 默认的一个样式
* /
  public final TypedArray obtainStyledAttributes(
            AttributeSet set, @StyleableRes int[] attrs, @AttrRes int defStyleAttr,
            @StyleRes int defStyleRes) {
        return getTheme().obtainStyledAttributes(
            set, attrs, defStyleAttr, defStyleRes);
    }

优先级 set>defStyleAttr>defStyleRes
defStyleAttr示例

<attr name="xin_tv_attr" format="reference"></attr>

  <style name="ActivityTheme" parent="Theme.AppCompat.Light">
        <item name="xin_tv_attr">@style/xin_tv_style</item> 
   </style>

 <style name="xin_tv_style">
        <item name="xin_color">#ffff00</item>
        <item name="xin_size">16sp</item>
    </style>

 TypedArray typedArray = obtainStyledAttributes(null, 
    R.styleable.xin_tv, R.attr.xin_tv_attr, R.style.xin_tv2);

1、资源文件定义
declare-styleable 与attr的区别
declare-styleable 相当于attr的集合

2、发现defStyleAttr设置在layout布局文件中不起作用,设置在Activity的theme上或app Theme

3、TypedArray的使用

int color2 = typedArray.getColor(R.styleable.xin_tv_xin_color, Color.BLUE);
 typedArray.recycle();

相关文章

  • obtainStyledAttributes理解

    参考:http://www.jianshu.com/p/61b79e7f88fc在使用 发现样式可在style中配...

  • Android 获取xml中的系统属性

    TypedArray ta = context.obtainStyledAttributes(attrs, new...

  • TypedArray流程分析

    Context#obtainStyledAttributes 在Context中的getTheme方法是抽象方法,...

  • 如何通过AndroidStudio调试源码?

    有时候,我们想查看源码知道例如setContentView、obtainStyledAttributes等方法是如...

  • 理解!理解!!理解!!!

    1、如何理解MVC设计模式 MVC是一种架构模式,M表示MOdel,V表示视图View,C表示控制器Control...

  • 理解“理解”

    教育不是灌输而是揭示 教育就是向智者揭示智慧,对愚者掩盖无知。——安布罗斯 比尔斯 我们的课堂教学要做的是设计表现...

  • 理解理解,再理解

    【今日悦读】 1214-潇潇 书名:财富自由之路 作者:李笑来 篇目:10~12节 收获: 1,人生三大坑 莫名其...

  • 理解不被理解

    一个人的坐在电脑前发呆,不知道搞什么,闭上眼睛,感觉自己很孤独。这种是内心的那种,很不是滋味,不知道什么时候自己才...

  • 理解你的理解理顺理解

    理解你的理解理顺你的理解 生活中因为缺乏准确具体到位的沟通和具体的实施方向和可落地的方式,因为信息传达...

  • 理解不理解

    对于更多人而言,或许他们更看重的是对与错,是与非。这种执念而且经常让一些人变了味,甚至格格不入。 而对于个人而言,...

网友评论

      本文标题:obtainStyledAttributes理解

      本文链接:https://www.haomeiwen.com/subject/qwfrrxtx.html