方法一(单一隐藏)
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 隐藏标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 隐藏状态栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
}
}
方法二(全部隐藏)
不管是继承AppCompatActivity还是Activity都适用:
在style.xml定义
<style name="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
//隐藏标题栏
<item name="windowNoTitle">true</item>
//隐藏状态栏
<item name="android:windowFullscreen">true</item>
</style>
在AndroidManifest.xml里面设置下theme属性:
android:theme="@style/Theme.AppCompat.NoActionBar"
透明状态栏,自定义状态栏,沉浸式状态栏
1.在style.xml定义
<style name="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
//隐藏标题栏
<item name="windowNoTitle">true</item>
//隐藏状态栏
// <item name="android:windowFullscreen">true</item>
//透明状态栏
<item name="android:windowTranslucentStatus">true</item>
</style>
2.在AndroidManifest.xml里面设置下theme属性
真正的沉浸式模式
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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);
}
}
}