context.obtainStyledAttributes()中的defStyleAttr和defStyleRes用于什么?

Fra*_*eng 10 android

当我办理登机手续QuickContactBadgeFrameLayout,我发现以下代码:

 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();
}
Run Code Online (Sandbox Code Playgroud)

我真的没有抓住defstyle0参数的含义obtainStyledAttributes().我查了参考,但仍然不知道它用于什么.

cit*_*onn 16

文档解释了它相当清楚,你有没有看到这部分:

defStyleAttr 当前主题中的一个属性,它包含对为StyledAttributes提供默认值的样式资源的引用.可以为0以查找默认值.

defStyleRes 为StyledAttributes提供默认值的样式资源的资源标识符,仅在defStyleAttr为0或在主题中找不到时使用.可以为0以查找默认值.

"可以为0,不寻找默认值." 如果将此值设置为0,那么它将不会尝试获取样式属性的默认值.它似乎有点违反直觉的,为什么重载这个方法,如果你可以只传递一个0 ...但我认为这是这样你就可以告诉它不看defStyleAttr为默认值,但告诉它在寻找defStyleRes为默认值,反之亦然.