【问题标题】:How to get color from theme (not current)?如何从主题(非当前)中获取颜色?
【发布时间】:2017-09-07 05:32:57
【问题描述】:

我有一些样式主题,可以选择。和setTheme(getThemeResId())BaseActivity。在我需要的任何地方,我都会通过?attr/colorPrimary 获得主色。但在设置中,我想显示所有主题颜色。喜欢getColor(R.attr.colorPrimary, R.style.AppTheme1)。如何做到这一点?

【问题讨论】:

    标签: android


    【解决方案1】:
    int[] attrs = { R.attr.colorPrimary, R.attr.colorAccent, R.attr.colorSecondary };
    TypedArray ta = getContext().obtainStyledAttributes(R.style.AppTheme1, attrs);
    int colorPrimary = ta.getColor(0, Color.BLACK);
    int colorAccent = ta.getColor(1, Color.BLACK);
    int colorSecondary = ta.getColor(2, Color.BLACK);
    Log.i("LOG", "colorPrimary as hex:" + Integer.toHexString(colorPrimary));
    

    【讨论】:

      猜你喜欢
      • 2021-06-05
      • 2017-01-13
      • 2019-03-07
      • 1970-01-01
      • 2014-05-17
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      • 2012-02-23
      相关资源
      最近更新 更多