What does defStyleAttr and defStyleRes in context.obtainStyledAttributes() used for?

后端 未结 1 391
春和景丽
春和景丽 2020-12-29 11:30

When I check QuickContactBadge in FrameLayout, I found following code:

 public QuickContactBadge(Context context, AttributeSet attr         


        
相关标签:
1条回答
  • 2020-12-29 11:48

    The documentation explains it pretty clearly, did you see this part:

    defStyleAttr An attribute in the current theme that contains a reference to a style resource that supplies defaults values for the StyledAttributes. Can be 0 to not look for defaults.

    defStyleRes A resource identifier of a style resource that supplies default values for the StyledAttributes, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults.

    "Can be 0 to not look for defaults." If you set this to 0 then it's not going to try to grab default values for the style attributes. It does seem a little counter-intuitive, why overload this method if you can just pass in a 0... but I think it is so you can tell it not to look in defStyleAttr for defaults but do tell it to look in defStyleRes for defaults, and vice versa.

    0 讨论(0)
提交回复
热议问题