整理記錄閱讀code以及修復BUG
---------
在Activity的onCreate進行setContentView之前,可以設置窗口的Feature,比如noTitle,全屏等。
調用方法:getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow即返回的是Window,在Window.java中對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;
}
所以必須要在setContentView之前,調用此函數。對於apk開發者,務必要重視。
Window的唯一實現者是PhoneWindow,因此requestFeature的具體實現可以查看PhoneWindow。
首先貼一下對於Window的註釋:
/**
* Abstract base class for a top-level window look and behavior policy. An
* instance of this class should be used as the top-level view added to the
* window manager. It provides standard UI policies such as a background, title
* area, default key processing, etc.
*
* <p>The only existing implementation of this abstract class is
* android.policy.PhoneWindow, which you should instantiate when needing a
* Window. Eventually that class will be refactored and a factory method
* added for creating Window instances without knowing about a particular
* implementation.
*/
public abstract class Window {
然後看PhoneWindow.java中對於requestFeature的實現
@Override
public boolean requestFeature(int featureId) {
if (mContentParent != null) {
throw new AndroidRuntimeException("requestFeature() must be called before adding content");
}
final int features = getFeatures();
if ((features != DEFAULT_FEATURES) && (featureId == FEATURE_CUSTOM_TITLE)) {
/* Another feature is enabled and the user is trying to enable the custom title feature */
throw new AndroidRuntimeException("You cannot combine custom titles with other title features");
}
if (((features & (1 << FEATURE_CUSTOM_TITLE)) != 0) &&
(featureId != FEATURE_CUSTOM_TITLE) && (featureId != FEATURE_ACTION_MODE_OVERLAY)) {
/* Custom title feature is enabled and the user is trying to enable another feature */
throw new AndroidRuntimeException("You cannot combine custom titles with other title features");
}
if ((features & (1 << FEATURE_NO_TITLE)) != 0 && featureId == FEATURE_ACTION_BAR) {
return false; // Ignore. No title dominates.
}
if ((features & (1 << FEATURE_ACTION_BAR)) != 0 && featureId == FEATURE_NO_TITLE) {
// Remove the action bar feature if we have no title. No title dominates.
removeFeature(FEATURE_ACTION_BAR);
}
return super.requestFeature(featureId);
}
Phonewindow會先對各種feature做處理,如果沒有任何處理,將調用父類,即Window.java的requestFeature。