本文包含3種隱藏頂部狀態(tài)欄及標題欄和一種隱藏Android 4.0平板底部狀態(tài)欄的方法,分享給大家供大家參考,具體內容如下
方法一
public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 隱藏標題欄 requestWindowFeature(Window.FEATURE_NO_TITLE); // 隱藏狀態(tài)欄 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); }}方法二
<!-- 同時隱藏狀態(tài)欄和標題欄 --><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>
方法三
<!-- Application theme. --><style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> <!-- 隱藏狀態(tài)欄 --> <item name="android:windowFullscreen">true</item> <!-- 隱藏標題欄 --> <item name="android:windowNoTitle">true</item></style>
注:
1、方法一中的兩段代碼要在setContentView()之前。
2、方法二只能同時隱藏狀態(tài)欄和標題欄。
3、方法一和方法二都只應用于單個Activity。方法三應用于整個程序。
對于運行Android 4.0以上系統(tǒng)的平板電腦,以上三種方法都不會隱藏屏幕下方的狀態(tài)欄,須做如下處理。
public class StartupActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_startup); /* * 隱藏運行Android 4.0以上系統(tǒng)的平板的屏幕下方的狀態(tài)欄 */ try { String ProcID = "79"; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) ProcID = "42"; // ICS // 需要root 權限 Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "service call activity " + ProcID + " s16 com.android.systemui" }); // WAS proc.waitFor(); } catch (Exception ex) { Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG).show(); } } @Override protected void onDestroy() { // TODO Auto-generated method stub /* * 恢復運行Android 4.0以上系統(tǒng)的平板的屏幕下方的狀態(tài)欄 */ try { Process proc = Runtime.getRuntime().exec(new String[] { "am", "startservice", "-n", "com.android.systemui/.SystemUIService" }); proc.waitFor(); } catch (Exception e) { e.printStackTrace(); } super.onDestroy(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.startup, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.action_exit: finish(); break; } return true; }}由于沒有了狀態(tài)欄,須在程序中提供退出程序的方法。
希望本文所述對大家學習Android軟件編程有所幫助。
新聞熱點
疑難解答
圖片精選