使用xml进行横屏
android:screenOrientation="landscape"
使用xml进行竖屏
android:screenOrientation="portrait"

隐藏标题栏xml
//隐藏标题栏

true

xml全屏

  <item name="android:windowFullscreen">true</item>

使用代码进行全屏

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

隐藏状态栏xml

    //隐藏状态栏
    <item name="android:windowFullscreen">true</item>

透明状态栏

            //透明状态栏
            <item name="android:windowTranslucentStatus">true</item>

代码隐藏状态栏

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) //隐藏状态栏 

代码显示状态栏

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) //显示状态栏

隐藏导航栏

代码隐藏导航栏

/**
     * 隐藏虚拟按键,并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键,并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }

隐藏状态栏和导航栏xml

   <item name="android:windowFullscreen">true</item>

代码隐藏状态栏和导航栏
在setContentView之前设置flag

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

2、一些手机比如华为的机子,大多都有虚拟按键即navigationbar,关于隐藏NavigationBar,网络上大多数的做法是设置一个flag,如下:

View decorView = getWindow().getDecorView();

decorView .setSystemUiVisibility(

 View.SYSTEM_UI_FLAG_LAYOUT_STABLE

 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar

 | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar

 | View.SYSTEM_UI_FLAG_IMMERSIVE);

实现沉浸式状态栏

 @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }

xml其它属性

         <!-- 状态栏颜色 -->
        <item name="colorPrimaryDark">@color/white</item>
        <!-- 导航栏沉浸 -->
        <item name="android:windowTranslucentNavigation">true</item>
        <!-- 状态栏沉浸-->
        <item name="android:windowTranslucentStatus">true</item>
        <!-- 亮色状态栏 -->
        <item name="android:windowLightStatusBar">true</item>
        <!-- 亮色导航栏 -->
        <item name="android:windowLightNavigationBar">true</item>

判断当前是横屏还是竖屏

public boolean isScreenOriatationPortrait() {
        return mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    }

发表评论

邮箱地址不会被公开。 必填项已用*标注