What does defStyleAttr and defStyleRes mean in the context of .obtainStyledAttributes ()?

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

  public QuickContactBadge(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.QuickContactBadge, defStyle, 0); mMode = a.getInt(com.android.internal.R.styleable.QuickContactBadge_quickContactWindowSize, QuickContact.MODE_MEDIUM); a.recycle(); init(); mBadgeBackground = getBackground(); } 

I really don't understand the value of the defstyle parameter and 0 in obtainStyledAttributes() . I looked at the link, but still do not know what it was used for.

+6
source share
1 answer

The documentation explains this quite clearly, you saw this part:

defStyleAttr An attribute of the current theme containing a reference to a style resource that supplies default values ​​for StyledAttributes. May be 0 so as not to look for default values.

defStyleRes The style resource resource identifier that supplies the default values ​​for StyledAttributes is used only if defStyleAttr is 0 or cannot be found in the topic. May be 0 so as not to look for default values.

"May be 0 so as not to look for default values." If you set the value to 0, then it will not try to get the default values ​​for the style attributes. This seems a bit counter-intuitive, so overload this method if you can just pass 0 ... but I think it is so that you can tell it not to look in defStyleAttr for default values, but should say so that it looks defStyleRes for default values ​​and vice versa.

+13
source

Source: https://habr.com/ru/post/893327/


All Articles