无法隐藏状态栏?

时间:2011-07-18 04:20:29

标签: android statusbar

我想在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);
}

它不起作用,状态栏始终可见。 有什么建议吗?谢谢!

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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发送消息即可。我希望你能判断两者的区别。

干杯。