您可以像这样设置背景颜色渐变:
Window window = activity.getWindow();
Drawable background = activity.getResources().getDrawable(R.drawable.gradient_theme);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(activity.getResources().getColor(R.color.transparent));
window.setNavigationBarColor(activity.getResources().getColor(R.color.transparent));
window.setBackgroundDrawable(background);
但它适用于 api 21 及更大版本
并且您的渐变主题在 drawables 中应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="@color/colorPrimary"
android:endColor="@color/color_primary_100"
android:type="linear" />
</shape>
</item>
</selector>