1

I have been struggling to obtain the styled attributes with the pre-lollipop API.

With lollipop, I use

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

This works flawlessly, I haven't found a similar way to do this below API version 21. (minAPI = 16)

I tried using the getTheme().obtainStyledAttributes(). But, I don't have the AttributeSet to provide to that method since I am using this inside an activity. Am I doing things completely wrong, or is resolving styled attributes not supported on API versions below 21?

4

1 に答える 1

2

このコードを試してください

  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を使用する必要があります。それだけです:)

于 2016-02-15T19:22:13.813 に答える