2011-07-18 311 views
0

我想在Android 3.1中隱藏狀態欄。 當用戶觸摸屏幕時,狀態欄將會顯示,2秒後將自動隱藏。無法隱藏狀態欄?

這裏我的代碼:

private static ViewFlipper flipper = null; 
private static ImageView ivScreenSnapshot = null; 

public static AbsoluteLayout managerField = null; 

public void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    managerField = new AbsoluteLayout(this); 

    flipper = new ViewFlipper(this) 
    { 
     @Override 
     public boolean onTouchEvent(MotionEvent event) 
     { 
      flipper.setSystemUiVisibility(View.STATUS_BAR_VISIBLE); 
      Thread t = new Thread() 
      { 
       public void run() 
       { 
        try { 
         sleep(2000); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
        Global.handler.post(new Runnable() 
        { 
         public void run() 
         { 
          flipper.setSystemUiVisibility(View.STATUS_BAR_HIDDEN); 
         } 
        }); 
       } 
      }; 
      t.start(); 
      return super.onTouchEvent(event); 
     } 
    }; 
    ivScreenSnapshot = new ImageView(this); 
    flipper.addView(managerField); 
    flipper.addView(ivScreenSnapshot); 
    setContentView(flipper); 
    flipper.setSystemUiVisibility(View.STATUS_BAR_HIDDEN); 
} 

它不工作,狀態欄始終可見。 有什麼建議嗎?謝謝!

回答

3

試試這個代碼

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="de.vogella.android.temperature" 
    android:versionCode="1" 
    android:versionName="1.0"> 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".Convert" 
       android:label="@string/app_name" 
       android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

</application> 
<uses-sdk android:minSdkVersion="9" /> 
</manifest> 
3

我已經嘗試了代碼,它完美地工作。爲了測試,我在onCreate()中調用了一個線程,並向處理程序發送了一條消息來隱藏狀態欄。以下是代碼示例。你可以根據你的需要使用它。

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.test); 

    Thread background = new Thread (new Runnable() 
    { 
     public void run() 
     { 
      try 
      { 
       Thread.sleep(4000); 
       progressHandler.sendMessage(progressHandler.obtainMessage()); 

      } 
      catch (InterruptedException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }); 

    // start the background thread 
    background.start(); 
} 

// handler for the background updating 
Handler progressHandler = new Handler() 
{ 
    public void handleMessage(Message msg) 
    { 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    } 
}; 

那麼,在onCreate()中使用Thread是沒有必要的。你可以在沒有線程的情況下完成。只需調用Thread.Sleep()並將消息發送給Handler即可。我希望你能判斷兩者的區別。

乾杯。

+0

謝謝大家, 我的代碼不工作,因爲我已經在manfest文件中聲明瞭android:minSdkVersion = 8。 現在,我編輯android:minSdkVersion = 12,它工作正常。 – Levanphong7887