在開發的過程中,為了增加更好的視圖效果,需要將當前Activity設定為全螢幕模式。幾經波折,順利搞定。在這裡分享給大家!
先看這段代碼:
@Override<br />public void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.main);<br />this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,<br />WindowManager.LayoutParams.FLAG_FULLSCREEN);<br />requestWindowFeature(Window.FEATURE_NO_TITLE);</p><p>}
運行程式,你會看到這樣的錯誤資訊:
ERROR/AndroidRuntime(690):<br />java.lang.RuntimeException:<br />Unable to start activity ComponentInfo:<br />android.util.AndroidRuntimeException:<br />requestFeature() must be called before adding content<br />
分析一下,先看sdk的文檔怎麼解釋的:
public boolean requestFeature (int featureId)<br />Since: API Level 1<br />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.<br />
大致意思,就是必須在調用setContentView()方法之前調用requestFeature (int featureId)方法。
提示:您是否注意到這句話:
You canot use other title features with FEATURE_CUSTOM_TITLE.
mygod,怎麼回事?別急,看文檔:
public static final int FEATURE_CUSTOM_TITLE<br />Since: API Level 1<br />Flag for custom title. You cannot combine this feature with other title features.<br />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) {<br /> return getWindow().requestFeature(featureId);<br /> }
這裡,requestFeature又調用了 Window類的requestFeature方法,刨根到底吧!看看Window類的requestFeature方法源碼:
/**<br /> * Enable extended screen features. This must be called before<br /> * setContentView(). May be called as many times as desired as long as it<br /> * is before setContentView(). If not called, no extended features<br /> * will be available. You can not turn off a feature once it is requested.<br /> * You canot use other title features with {@link #FEATURE_CUSTOM_TITLE}.<br /> *<br /> * @param featureId The desired features, defined as constants by Window.<br /> * @return The features that are now set.<br /> */<br /> public boolean requestFeature(int featureId) {<br /> final int flag = 1<<featureId;<br /> mFeatures |= flag;<br /> mLocalFeatures |= mContainer != null ? (flag&~mContainer.mFeatures) : flag;<br /> return (mFeatures&flag) != 0;<br /> }
既然sdk文檔讓我們這麽做,就照辦嘍,修改代碼如下:
@Override<br />public void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />requestWindowFeature(Window.FEATURE_NO_TITLE);<br />setContentView(R.layout.main);<br />this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,<br />WindowManager.LayoutParams.FLAG_FULLSCREEN);<br />}
好啦,運行正常!到達全屏的效果。歡呼一把.....
試問一下,是不是還有什麼辦法修改代碼,到達同樣的效果?還要看看sdk文檔,關於setFlags方法的說明:
public void setFlags (int flags, int mask)<br />Since: API Level 1<br />Set the flags of the window, as per the WindowManager.LayoutParams flags.<br />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.<br />
大致意思,請允許我以下面例子為證:
@Override<br />public void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,<br />WindowManager.LayoutParams.FLAG_FULLSCREEN);<br />requestWindowFeature(Window.FEATURE_NO_TITLE);<br />setContentView(R.layout.main);<br />/*this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,<br />WindowManager.LayoutParams.FLAG_FULLSCREEN);*/<br />}
沒錯,我只是調換了代碼的位置。運行代碼,沒有報錯。但是,點擊視圖中的控制項如Button沒有任何反應。那麼,你應該明白上面的意思就是調用setFlags方法之前,任何修飾window(視窗)如requestWindowFeature需要先調用。
其實,我更加習慣這樣設定全螢幕模式:
@Override<br />public void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />requestWindowFeature(Window.FEATURE_NO_TITLE);<br />this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,<br />WindowManager.LayoutParams.FLAG_FULLSCREEN);<br />setContentView(R.layout.main);<br />}
在代碼中,定義全屏運行程式之後螢幕仍會出現瞬間的非全螢幕模式,如何辦?對,xml檔案!!!修改manifest.xml檔案如下:
<activity android:name=".ActMain"<br /> android:label="@string/app_name"<br /> android:theme="@android:style/Theme.NoTitleBar.Fullscreen"><br /> <intent-filter><br /> <action android:name="android.intent.action.MAIN" /><br /> <category android:name="android.intent.category.LAUNCHER" /><br /> </intent-filter><br /> </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,到此為止!