设置应用全屏的几种方式

1. 去掉标题栏+全屏显示+导航栏半透明

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // todo 去掉标题栏
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        // todo 全屏显示以及导航栏半透明
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // todo 还原设置
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }
}

2. 去掉标题栏+全屏显示+设置导航栏颜色

public class MainActivity extends AppCompatActivity {

    private int mNavigationBarColor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // todo 去掉标题栏
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        // todo 全屏显示
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // todo 设置导航栏颜色
        mNavigationBarColor = getWindow().getNavigationBarColor();
        getWindow().setNavigationBarColor(Color.TRANSPARENT);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // todo 还原设置
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setNavigationBarColor(mNavigationBarColor);
    }
}

3. 对DecorView进行设置

public class MainActivity extends AppCompatActivity {

    private int mSystemUiVisibility;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View decorView = getWindow().getDecorView();
        // todo 获取当前的ui状态
        mSystemUiVisibility = decorView.getSystemUiVisibility();
        // todo 全屏
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // todo 还原ui状态
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(mSystemUiVisibility);
    }
}

4. 清单文件设置主题

  1. 在/res/values/styles.xml中自定义全屏主题
    <style name="FullScreen" parent="Theme.AppCompat.Light.NoActionBar">
        <!--窗口全屏-->
        <item name="android:windowFullscreen">true</item>
        <!--导航栏半透明-->
        <item name="android:windowTranslucentNavigation">true</item>
    </style>
  1. 在清单文件中设置主题
        <activity
            android:name=".MainActivity"
            android:theme="@style/FullScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
上一篇:Android GUI 系统总结之Window


下一篇:Dialog/PopupWindow/Toast 到底该怎么选