requestWindowFeature該如何使用呢?根據android developers中的介紹和如下程序進行學習。具體代碼以下: android
package com.example.reqestfutertest; app
import android.app.Activity; ide
import android.os.Bundle; 佈局
import android.os.Handler; 學習
import android.os.Message; this
import android.view.Window; spa
import android.view.WindowManager; 線程
public class MainActivity extends Activity { get
@Override it
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
// requestWindowFeature();的取值
// 1.DEFAULT_FEATURES:系統默認狀態,通常不須要指定
// 2.FEATURE_CONTEXT_MENU:啓用ContextMenu,默認該項已啓用,通常無需指定
// 3.FEATURE_CUSTOM_TITLE:自定義標題。當須要自定義標題時必須指定。如:標題是一個按鈕時
// 4.FEATURE_INDETERMINATE_PROGRESS:不肯定的進度
// 5.FEATURE_LEFT_ICON:標題欄左側的圖標
// 6.FEATURE_NO_TITLE:無標題
// 7.FEATURE_OPTIONS_PANEL:啓用「選項面板」功能,默認已啓用。
// 8.FEATURE_PROGRESS:進度指示器功能
// 9.FEATURE_RIGHT_ICON:標題欄右側的圖標
/** FEATURE_INDETERMINATE_PROGRESS:不肯定的進度 */
// showFEATURE_INDETERMINATE_PROGRESS();
/** FEATURE_CUSTOM_TITLE:自定義標題 */
// showFEATURE_CUSTOM_TITLE();
/** FEATURE_LEFT_ICON:標題欄左側的圖標 */
// showFEATURE_LEFT_ICON();
/** FEATURE_NO_TITLE:無標題 */
// showFEATURE_NO_TITLE();
/** FEATURE_PROGRESS:進度條 */
showFEATURE_PROGRESS();
}
private void showFEATURE_INDETERMINATE_PROGRESS() {
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
R.layout.progress);
// 必須得加上不然顯示不出效果 能夠經過這個在之後設置顯示或隱藏
setProgressBarIndeterminateVisibility(true);
}
private void showFEATURE_CUSTOM_TITLE() {
// 自定義標題,當須要自定義標題時必須指定
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.customtitle);
}
private void showFEATURE_LEFT_ICON() {
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.activity_main);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
R.drawable.ic_launcher);
}
private void showFEATURE_NO_TITLE() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// 加上這句設置爲全屏 不加則只隱藏title
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
private void showFEATURE_PROGRESS() {
requestWindowFeature(Window.FEATURE_PROGRESS);
setProgressBarVisibility(true);
setContentView(R.layout.activity_main);
setTitle("");
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
// 經過線程來改變ProgressBar的值
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
Message m = new Message();
m.what = (i + 1) * 20;
MainActivity.this.myMessageHandler.sendMessage(m);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
Handler myMessageHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// 設置標題欄中前景的一個進度條進度值
setProgress(100 * msg.what);
// 設置標題欄中後面的一個進度條進度值
setSecondaryProgress(100 * msg.what + 10);
super.handleMessage(msg);
}
};
}
這裏應該注意的問題是,必定要先寫 requestWindowFeature屬性,而後再加載佈局,不然就會報運行時錯誤。