在运行时查询colorControlHighlight值将返回负值

时间:2019-05-30 10:09:31

标签: android styles android-theme android-styles ripple

我正在尝试在运行时获得波纹的颜色:

    <ImageView
        android:id="@+id/close_button"

...
 android:padding="12dp"
        android:alpha="0"
        android:theme="@style/ripple_theme"
        android:background="?attr/selectableItemBackground"
        android:contentDescription="@string/og_close_icon_a11y"
        android:visibility="gone"
        app:srcCompat="@drawable/quantum_gm_ic_close_vd_theme_24"
        tools:ignore="InconsistentLayout"/>

values.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <style name="ripple_theme">
    <item name="colorControlHighlight">@color/google_yellow100</item>
  </style>

</resources>

另外,我是否需要将Alpha设置为10%,或者这是波纹自动设置的值?

我试图在运行时检索android.R.attr.colorControlHighlight颜色值,但我总是得到一个负值。

我该如何解决?

int result = 0;
View closeBtn = findViewById(R.id.close_button);
TypedValue outValue = new TypedValue();

Resources.Theme theme = closeBtn.getContext().getTheme();
boolean wasResolved =
    theme.resolveAttribute(android.R.attr.colorControlHighlight, outValue, true);
if (wasResolved) {
  result =
      outValue.resourceId == 0
          ? outValue.data
          : ContextCompat.getColor(getContext(), outValue.resourceId);
}
result = result + 0;

0 个答案:

没有答案