Android应用程序全屏(沉浸式)隐藏标题栏和虚拟键
Android隐藏标题栏 +底部虚拟键Navigation Bar实现全屏
一、标题栏隐藏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
二、隐藏了虚拟键
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);//成功的隐藏了虚拟键
三、Demo
布局 activity_main.xml
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <TextView 12 android:id="@+id/textView1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="全屏演示" /> 16 17 <Button 18 android:id="@+id/button1" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:layout_below="@+id/textView1" 22 android:layout_marginTop="59dp" 23 android:layout_toRightOf="@+id/textView1" 24 android:text="展示全屏" /> 25 26 <Button 27 android:id="@+id/button2" 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:layout_alignRight="@+id/button1" 31 android:layout_below="@+id/button1" 32 android:text="退出全屏" /> 33 34 <Button 35 android:id="@+id/button3" 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content" 38 android:layout_alignRight="@+id/button2" 39 android:layout_below="@+id/button2" 40 android:text="退出程序" /> 41 42 </RelativeLayout>
scourceCode:
1 package com.example.fullscreen; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.app.ActivityManager; 6 import android.view.Menu; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.view.Window; 10 import android.view.WindowManager; 11 import android.widget.Button; 12 13 public class MainActivity extends Activity { 14 15 Window window = null; 16 Button button2 = null; 17 Button button3 = null; 18 Button button1 = null; 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 23 window = getWindow(); 24 /*方法一:设置全屏,虚拟键仍然存在。*/ 25 requestWindowFeature(Window.FEATURE_NO_TITLE); 26 window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 27 WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏,虚拟键仍然存在。 28 29 setContentView(R.layout.activity_main); 30 31 32 33 /*方法二:*/ 34 button1 = (Button) findViewById(R.id.button1); /*设置全屏*/ 35 button1.setOnClickListener(new OnClickListener() { 36 37 /*获取当前系统的android版本号*/ 38 int currentapiVersion=android.os.Build.VERSION.SDK_INT; 39 @Override 40 public void onClick(View v) { 41 if (currentapiVersion >= 14) { 42 window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);//全屏显示 43 } 44 } 45 }); 46 button2 = (Button) findViewById(R.id.button2); /*退出全屏*/ 47 button2.setOnClickListener(new OnClickListener() { 48 /*触摸屏幕任意位置,即可退出全屏模式*/ 49 @Override 50 public void onClick(View v) { 51 window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//可以不要这行代码 52 } 53 }); 54 button3 = (Button) findViewById(R.id.button3); 55 button3.setOnClickListener(new OnClickListener() { 56 57 @Override 58 public void onClick(View v) { 59 /*关闭应用程序*/ 60 finish(); //退出当前activity,OK 61 } 62 }); 63 64 } 65 66 67 68 69 70 71 @Override 72 public boolean onCreateOptionsMenu(Menu menu) { 73 // Inflate the menu; this adds items to the action bar if it is present. 74 getMenuInflater().inflate(R.menu.main, menu); 75 return true; 76 } 77 78 }
四、运行结果


浙公网安备 33010602011771号