http://www.xuebuyuan.com/558284.htmlhtml
方法一java
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); } }
方法二android
<!-- 同時隱藏狀態欄和標題欄 --> <activity android:name="com.ysj.demo.MainActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
方法三app
<!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> <!-- 隱藏狀態欄 --> <item name="android:windowFullscreen">true</item> <!-- 隱藏標題欄 --> <item name="android:windowNoTitle">true</item> </style>
注:ide
一、方法一中的兩段代碼要在setContentView()以前。ui
二、方法二隻能同時隱藏狀態欄和標題欄。spa
三、方法一和方法二都只應用於單個Activity。方法三應用於整個程序。3d
對於運行Android 4.0以上系統的平板電腦,以上三種方法都不會隱藏屏幕下方的狀態欄,須作以下處理。code
public class StartupActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_startup); /* * 隱藏運行Android 4.xxx系統的平板的屏幕下方的狀態欄須要root權限 */ closeBar(); } @Override protected void onDestroy() { showBar(); super.onDestroy(); } /** * 關閉Android導航欄,實現全屏 */private void closeBar() { try { String command; command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib service call activity 42 s16 com.android.systemui"; ArrayList<String> envlist = new ArrayList<String>(); Map<String, String> env = System.getenv(); for (String envName : env.keySet()) { envlist.add(envName + "=" + env.get(envName)); } String[] envp = envlist.toArray(new String[0]); Process proc = Runtime.getRuntime().exec( new String[] { "su", "-c", command }, envp); proc.waitFor(); } catch (Exception ex) { // Toast.makeText(getApplicationContext(), ex.getMessage(), // Toast.LENGTH_LONG).show(); } } /** * 顯示導航欄 */public static void showBar() { try { String command; command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib am startservice -n com.android.systemui/.SystemUIService"; ArrayList<String> envlist = new ArrayList<String>(); Map<String, String> env = System.getenv(); for (String envName : env.keySet()) { envlist.add(envName + "=" + env.get(envName)); } String[] envp = envlist.toArray(new String[0]); Process proc = Runtime.getRuntime().exec( new String[] { "su", "-c", command }, envp); proc.waitFor(); } catch (Exception e) { e.printStackTrace(); } } }
因爲沒有了狀態欄,須在程序中提供退出程序的方法。orm