1. 程式人生 > >Android中自定義頂部狀態列顏色

Android中自定義頂部狀態列顏色

public class StatusBarUtils {
    public static void setWindowStatusBarColor(Activity activity, int colorResId) {
        try {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

                Window window = activity.getWindow();

                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

                window.setStatusBarColor(activity.getResources().getColor(colorResId));



                //底部導航欄
//window.setNavigationBarColor(activity.getResources().getColor(colorResId)); } } catch (Exception e) { e.printStackTrace(); } } }

在activity中進行設定:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void
onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); StatusBarUtils.setWindowStatusBarColor(MainActivity.this,R.color.colorPrimary); setContentView(R.layout.activity_main); } }