gpt4 book ai didi

android - context.obtainStyledAttributes() 中的 defStyleAttr 和 defStyleRes 有什么用?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:53 32 4
gpt4 key购买 nike

当我检查 FrameLayout 中的 QuickContactBadge 时,我发现了以下代码:

 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();
}

我不太明白 obtainStyledAttributes()defstyle0 参数的含义。我查了引用资料,但仍然不知道它的用途。

最佳答案

documentation说:

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.

“可以为 0 以不查找默认值。”如果将其设置为 0,则它不会尝试获取样式属性的默认值。这似乎有点违反直觉,如果你可以传入一个 0 为什么要重载这个方法......但我认为这是这样你可以告诉它不要在 defStyleAttr 中查找默认值但是 < em>do 告诉它在 defStyleRes 中查找默认值,反之亦然。

关于android - context.obtainStyledAttributes() 中的 defStyleAttr 和 defStyleRes 有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6784854/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com