您的位置:首页 > 移动开发 > Android开发

android开发 全屏 隐藏电池等信息

2012-08-06 19:31 489 查看
Android我们可以使用以下方式设置手机全屏有以下几种方式:
要全屏我们首先要将title栏和电池、时间信息栏全部隐藏:
1、隐藏title栏
使用将activity的title导航栏给去掉

requestWindowFeature(Window.FEATURE_NO_TITLE);(隐藏title导航栏)


2、隐藏时间、电池栏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
隐藏信息状态栏

看一下我们实际使用

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        //全屏
        setContentView(new DrawBitmapTest(this)); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

运行以上程序会产生如下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dream.sur/com.dream.sur.draw.SurfaceViewTestActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding contentCaused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:189)
at android.app.Activity.requestWindowFeature(Activity.java:2735)

我们查看源码提示

Enable extended screen features.  This must be called before setContentView().  
Note that some flags must be set before the window decoration is created (by the first call to {@link #setContentView(View, android.view.ViewGroup.LayoutParams)

意思是说 requestWindowFeature 必须在 setContentView() 这个方法之前调用,后面使之 setFlags 的设置需要在 setContentView();

所以正确的方式是应为:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(new DrawBitmapTest(this));
    }

将title栏和时间电池信息状态栏给隐藏掉



问题又来了,细心的童鞋应该会发现直接启动activity的时候,会有一瞬间title显示出来了,咋办呢?

我们可以修改activity的启动参数,在AndroidManifest.xml中修改如下代码

<activity
     android:name=".draw.SurfaceViewTestActivity"
     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>

如果想要仅去除title栏留下时间和电池信息状态栏可以将以上xml theme换成:

android:theme="@android:style/Theme.NoTitleBar"

至此android全屏的设置已经ok
另:查看源码可得知:其实requestWindowFeature就是由getWindow().requestFeature(id)实现的

public final boolean requestWindowFeature(int featureId) {
        return getWindow().requestFeature(featureId);
}
转自:http://dreamthinking.blog.163.com/blog/static/205181171201251973349985/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: