在开发的过程中,为了增加更好的视图效果,需要将当前Activity设置为全屏模式。几经波折,顺利搞定。在这里分享给大家!
先看这段代码:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); }
运行程序,你会看到这样的错误信息:
ERROR/AndroidRuntime(690): java.lang.RuntimeException: Unable to start activity ComponentInfo: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
分析一下,先看sdk的文档怎么解释的:
public boolean requestFeature (int featureId) Since: API Level 1 Enable extended screen features. This must be called before setContentView(). May be called as many times as desired as long as it is before setContentView(). If not called, no extended features will be available. You can not turn off a feature once it is requested. You canot use other title features with FEATURE_CUSTOM_TITLE.
大致意思,就是必须在调用setContentView()方法之前调用requestFeature (int featureId)方法。
提示:您是否注意到这句话:
You canot use other title features with FEATURE_CUSTOM_TITLE.
mygod,怎么回事?别急,看文档:
public static final int FEATURE_CUSTOM_TITLE Since: API Level 1 Flag for custom title. You cannot combine this feature with other title features. Constant Value: 7 (0x00000007)
yes,告诉您不可以设置FEATURE_CUSTOM_TITLE与其他特性联合。这个属性只可以单独用,例如下面做法是错误的。
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE | Window.*);
试想一下,这中做法还是有道理的。本身Window.FEATURE_CUSTOM就是让用户自定义的,何必添加Window.*。
查看android源码可以知道,requestFeature (int featureId)方法这样的:
public final boolean requestWindowFeature(int featureId) { return getWindow().requestFeature(featureId); }
这里,requestFeature又调用了 Window类的requestFeature方法,刨根到底吧!看看Window类的requestFeature方法源码:
/** * Enable extended screen features. This must be called before * setContentView(). May be called as many times as desired as long as it * is before setContentView(). If not called, no extended features * will be available. You can not turn off a feature once it is requested. * You canot use other title features with {@link #FEATURE_CUSTOM_TITLE}. * * @param featureId The desired features, defined as constants by Window. * @return The features that are now set. */ public boolean requestFeature(int featureId) { final int flag = 1<<featureId; mFeatures |= flag; mLocalFeatures |= mContainer != null ? (flag&~mContainer.mFeatures) : flag; return (mFeatures&flag) != 0; }
既然sdk文档让我们这麽做,就照办喽,修改代码如下:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); }
好啦,运行正常!到达全屏的效果。欢呼一把.....
试问一下,是不是还有什么办法修改代码,到达同样的效果?还要看看sdk文档,关于setFlags方法的说明:
public void setFlags (int flags, int mask) Since: API Level 1 Set the flags of the window, as per the WindowManager.LayoutParams flags. Note that some flags must be set before the window decoration is created (by the first call to setContentView(View, android.view.ViewGroup.LayoutParams) or getDecorView(): FLAG_LAYOUT_IN_SCREEN and FLAG_LAYOUT_INSET_DECOR. These will be set for you based on the windowIsFloating attribute.
大致意思,请允许我以下面例子为证:
@Override 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(R.layout.main); /*this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);*/ }
没错,我只是调换了代码的位置。运行代码,没有报错。但是,点击视图中的控件如Button没有任何反应。那么,你应该明白上面的意思就是调用setFlags方法之前,任何修饰window(窗口)如requestWindowFeature需要先调用。
其实,我更加习惯这样设置全屏模式:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); }
在代码中,定义全屏运行程序之后屏幕仍会出现瞬间的非全屏模式,如何办?对,xml文件!!!修改manifest.xml文件如下:
<activity android:name=".ActMain" 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>
如果,你只是想设置显示视图没有title的话而不想隐藏状态栏的话,可以使用Theme.NoTitleBar。即:
android:theme="@android:style/Theme.NoTitleBar">另外,说一下setFlags()与addFlags()方法之间的联系与区别。这两个方法都是Window类的方法。
addFlags()源码:
public void addFlags(int flags) { setFlags(flags, flags); }可以看出调用setFlags()方法,但是addFlags()参数只有一个。换句话说,代码:
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);可以修改为: this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);ok,到此为止!
本文详细介绍了在Android应用中设置全屏模式的具体步骤与注意事项,包括如何正确使用requestWindowFeature和setFlags方法,并提供了实现全屏效果的多种方法。
1万+

被折叠的 条评论
为什么被折叠?



