Android 去掉标题栏,状态栏,导航栏

一:标题栏

 

1继承Activity有效

1.1requestWindowFeature(Window.FEATURE_NO_TITLE);   

注意这句一定要写在setContentView()方法的前面,不然会报错的

 

1.2在manifest.xml中改Theme

 android:theme="@android:style/Theme.NoTitleBar"

 

2继承AppCompatActivity有效

 

2.1第一种:代码中getSupportActionBar().hide();

2.2第二种:在manifest.xml中改Theme

 

 android:theme="@style/Theme.AppCompat.Light.NoActionBar"

 

第一种会先出现标题栏,然后再消失,因为是在activity的oncreate方法中定义的,第二种相对第一种比较好一些,不会出现这种情况。

 

二;取消状态栏,和标题栏(通过顶部往下滑动还是可以出来状态栏的

继承Activity有效

requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

第二种
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

继承AppCompatActivity有效:

 

然后对应的activity设为这个主题

实现全屏:Android4.4以上包括4.4,

 

 @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus)
        {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE//保持布局状态
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION//可以显示在导航栏后面
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION//隐藏导航栏
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN//可以显示在状态栏后面
                            | View.SYSTEM_UI_FLAG_FULLSCREEN//隐藏状态栏
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);//显示状态栏后几秒后消失
        }

    }


也可放在oncreate里面setContentView之前

 

SYSTEM_UI_FLAG_IMMERSIVE 和 SYSTEM_UI_FLAG_IMMERSIVE_STICKY两种区别:

前者是将 bar 唤出后不再消失,后者是将 bar 唤出后几秒就消失,后者不触发 Listener。

获取状态栏和导航栏高度

 

方法
/**
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

     * 获取状态栏高度
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen",
                "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**
     * 获取导航栏高度
     * @param context
     * @return
     */
    public static int getDaoHangHeight(Context context) {
        int result = 0;
        int resourceId=0;
        int rid = context.getResources().getIdentifier("config_showNavigationBar", "bool", "android");
        if (rid!=0){
             resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
            CMLog.show("高度:"+resourceId);
            CMLog.show("高度:"+context.getResources().getDimensionPixelSize(resourceId) +"");
            return context.getResources().getDimensionPixelSize(resourceId);
        }else
      

 

系统工具栏显示变化的响应

 

  注册一个 View.OnSystemUiVisibilityChangeListener 来使界面同步变化,可以在 onCreate() 方法中添加以下代码:

View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
        (new View.OnSystemUiVisibilityChangeListener() {
    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        // Note that system bars will only be "visible" if none of the
        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
            // TODO: The system bars are visible. Make any desired
            // adjustments to your UI, such as showing the action bar or
            // other navigational controls.
        } else {
            // TODO: The system bars are NOT visible. Make any desired
            // adjustments to your UI, such as hiding the action bar or
            // other navigational controls.
        }
    }
});

 

 

 

你可能感兴趣的:(Android 去掉标题栏,状态栏,导航栏)