Android 全屏化-即:透明化状态栏-也有人误喊它“沉浸式”

遇到的问题:粘贴大师郭霖的全屏化透明状态栏,大师说很多人误叫它“沉浸式”
我的原代码:

package com.lz.test001;

import android.graphics.Color;
import android.os.Build;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
    }
}

报错内容:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.hide()' on a null object reference

解决办法:把“actionBar.hide();”给try

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        ActionBar actionBar = getSupportActionBar();
        try {
            actionBar.hide();		//这行给踹了即可(如果还不行-另请高明)
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

如果要在FragmentActivity中实现则要添加俩个方法:
一样记得要把“actionBar.hide();”给try

	public ActionBar getSupportActionBar() {
	        return supportActionBar;
	    }

    public void setSupportActionBar(ActionBar supportActionBar) {
        this.supportActionBar = supportActionBar;
    }

效果:
Android 全屏化-即:透明化状态栏-也有人误喊它“沉浸式”_第1张图片
本人AS版本3.2.1
模拟器Android版本9.0
(如果帮到你了请留言,如果没帮到请私我邮箱:[email protected] 反正我也不回)

你可能感兴趣的:(Bug)