Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

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

When I check QuickContactBadge in FrameLayout, I found 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 don't really catch the meaning of defstyle and 0 parameter in obtainStyledAttributes(). I have looked up reference, but still don't know what it used for.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The documentation says:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...