棒棒糖前的 getTheme().resolveAttribute() 替代方案

getTheme().resolveAttribute() alternative on pre-lollipop

我一直在努力获取棒棒糖前的样式属性API。

对于棒棒糖,我使用

final TypedValue statusBarColor = new TypedValue();
getTheme().resolveAttribute(android.R.attr.colorPrimaryDark, statusBarColor, true);
STATUS_BAR_COLOR = ContextCompat.getColor(this, statusBarColor.resourceId);

这完美无缺,我在 API 版本 21 下还没有找到类似的方法。(minAPI = 16)

我尝试使用 getTheme().obtainStyledAttributes()。但是,我没有为该方法提供的 AttributeSet,因为我在 activity 中使用它。我做的事情是完全错误的,还是正在解析 API 21 以下版本不支持的样式属性?

试试这个代码

  TypedValue typedValue = new TypedValue();
  getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
  STATUS_BAR_COLOR = ContextCompat.getColor(this, typedValue.resourceId);

不需要android.R.attr.colorPrimaryDark,而是应该使用R.attr.colorPrimaryDark 就这些:)