如何在 .setBackgroundDrawable 中使用资源颜色 ID

fgo*_*gle 0 android android-resources android-drawable

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

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


window.setStatusBarColor(?);
Run Code Online (Sandbox Code Playgroud)

什么应该放在括号内以引用R.color.aR.color.b

ran*_*ndy 5

传递颜色的RGB值

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.rgb(248, 248, 248)));
Run Code Online (Sandbox Code Playgroud)

传递资源ID

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(getContext(),R.color.primary)));
Run Code Online (Sandbox Code Playgroud)