如何通过 .setBackgroundDrawable 使用资源颜色 ID

How to use a resource color id with .setBackgroundDrawable

我正在尝试使用我在 colors.xml 文件中定义的两种颜色为工具栏和状态栏着色。

((AppCompatActivity) getActivity())
                .getSupportActionBar()
                .setBackgroundDrawable(?);


window.setStatusBarColor(?);

为了引用 R.color.aR.color.b ,括号内应该放什么?

传递颜色的 RGB 值

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.rgb(248, 248, 248)));

传递资源id

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(getContext(),R.color.primary)));

它应该是 setBackgroundResource(),而不是可绘制对象。