【问题标题】:getColorStateList has been deprecatedgetColorStateList 已被弃用
【发布时间】:2015-09-02 17:43:41
【问题描述】:

我在这里遇到了问题。我刚刚从sdk 22更新到23,之前版本的“getColorStateList()”已经被弃用了。

我的代码是这样的

seekBar.setProgressTintList(getResources().getColorStateList(R.color.bar_green));
valorslide.setTextColor(getResources().getColorStateList(R.color.text_green));

旧的“getColorStateList”是

getColorStateList(int id)

还有一个新的

getColorStateList(int id, Resources.Theme theme)

如何使用 Theme 变量?提前致谢

【问题讨论】:

    标签: android android-theme


    【解决方案1】:

    虽然 anthonycr 的答案有效,但写起来要紧凑得多

    ContextCompat.getColorStateList(context, R.color.haml_indigo_blue);
    

    【讨论】:

    • 这很酷!除了 seekbar 方法,还有什么类似的吗?我与seekBar.setProgressTintList() 有兼容性问题(通过安东尼的回答解决)(我真的不需要这个,这是为了知识)
    • 如何使用自定义颜色而不是资源颜色制作ColorStateList
    【解决方案2】:

    Theme 对象是用于设置颜色状态列表样式的主题。如果您没有对单个资源使用任何特殊主题,则可以传递null 或当前主题,如下所示:

    TextView valorslide; // initialize
    SeekBar seekBar; // initialize
    Context context = this;
    Resources resources = context.getResources();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        seekBar.setProgressTintList(resources.getColorStateList(R.color.bar_green, context.getTheme()));
        valorslide.setTextColor(resources.getColorStateList(R.color.text_green, context.getTheme()));
    } else {
        seekBar.setProgressTintList(resources.getColorStateList(R.color.bar_green));
        valorslide.setTextColor(resources.getColorStateList(R.color.text_green));
    }
    

    如果不关心主题,直接传null即可:

    getColorStateList(R.color.text_green, null)
    

    See the documentation for more explanation.注意,您只需要在API 23(Android Marshmallow)及以上版本上使用新版本即可。

    【讨论】:

    • 或者,坚持使用已弃用的版本,除非您的 minSdkVersion 为 23 或更高版本,否则无论如何您都需要在旧设备上使用已弃用的版本。
    • @CommonsWare 我应该指出这一点,并更新以反映该信息。谢谢。
    • 我之前尝试过 null 并且它崩溃了。我现在尝试了这两种方法(null 和 getTheme()),但它仍然崩溃 14635-14635/golden.imper.csystemhelper E/MessageQueue-JNI﹕ java.lang.NoSuchMethodError: No virtual method getColorStateList(ILandroid/content/res/Resources$Theme;)Landroid/content/res/Co‌​lorStateList; in class Landroid/content/res/Resources; or its super classes (declaration of 'android.content.res.Resources' appears in /system/framework/framework.jar)
    • @fkchaud 您需要在 API 23 下方使用该方法之前检查 API 版本,正如我在更新的答案中所说的那样,因为如果您尝试在 pre- 上使用更新的方法,它将引发该错误安卓 M 版本。此外,请确保您正在针对 API 23 进行编译。
    • @anthonycr 就是这样,我在 API 21 中对其进行了测试,这就是它崩溃的原因。现在可以了。谢谢两位!
    【解决方案3】:

    如果您使用它们,您将失去所有样式。对于旧版本,您应该动态创建ColorStateList,这是保持您的样式的主要机会。

    这适用于所有版本

    layout.setColorStateList(buildColorStateList(this,
       R.attr.colorPrimaryDark, R.attr.colorPrimary)
    );
    
    
    public ColorStateList buildColorStateList(Context context, @AttrRes int pressedColorAttr, @AttrRes int defaultColorAttr){
        int pressedColor = getColorByAttr(context, pressedColorAttr);
        int defaultColor = getColorByAttr(context, defaultColorAttr);
    
        return new ColorStateList(
                new int[][]{
                        new int[]{android.R.attr.state_pressed},
                        new int[]{} // this should be empty to make default color as we want
                }, new int[]{
                pressedColor,
                defaultColor
        }
        );
    }
    
    @ColorInt
    public static int getColorByAttr(Context context, @AttrRes int attrColor){
    
        if (context == null || context.getTheme() == null)
            return -1;
    
        Resources.Theme theme = context.getTheme();
        TypedValue typedValue = new TypedValue();
    
        theme.resolveAttribute(attrColor, typedValue,true);
    
        return typedValue.data;
    } 
    

    【讨论】:

      【解决方案4】:

      还有一种更新的方法:

      AppCompatResources.getColorStateList(context, R.color.bar_green)
      

      这会保持对已膨胀的 ColorStateList 缓存的弱引用,如果加载失败,则会回退到 ContextCompat.getColorStateList

      【讨论】:

        【解决方案5】:

        您需要使用 ContextCompat.getColor(),它是 Support V4 库的一部分(因此它适用于所有以前的 API)。

        ContextCompat.getColor(context, R.color.my_color)
        

        【讨论】:

          猜你喜欢
          • 2019-04-04
          • 2018-12-12
          • 2014-09-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-21
          • 2021-02-19
          相关资源
          最近更新 更多