学而实习之 不亦乐乎

Android:requestWindowFeature() 使用详解

2021-11-03 16:11:58

Android 开发中经常会在 setContentView(R.layout.main) 前设置 requestWindowFeature(featureId)。其作用是需要软件全屏显示、自定义标题(如,使用按钮等控件)等。

requestWindowFeature(featrueId),它的功能是启用窗体的扩展特性。参数是 Window 类中定义的常量。

一、枚举常量(android.view.Window)

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:标题栏右侧的图标

二、常量使用详解

1.DEFAULT_FEATURES:系统默认状态,一般不需要指定
2.FEATURE_CONTEXT_MENU:启用ContextMenu,默认该项已启用,一般无需指定
3.FEATURE_CUSTOM_TITLE:自定义标题。当需要自定义标题时必须指定。

    this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.main);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title)

    自定义标题完成,它是一个xml文件布局(title.xml),如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" >
     
      <ImageView android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:src="@drawable/icon"/>
       <TextView android:id="@+id/text" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_alignParentLeft="true" 
            android:text="文本" />      
    </LinearLayout>

4.FEATURE_INDETERMINATE_PROGRESS:不确定的进度

    [1]Java代码

    this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.main);

    getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, R.layout.progress);
    setProgressBarIndeterminateVisibility(true);

    [2]progress.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
        <ProgressBar android:id="@+id/progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"  
            android:layout_gravity="center_vertical"
            style="?android:attr/progressBarStyleSmallTitle">
        </ProgressBar>
    </LinearLayout>

5.FEATURE_LEFT_ICON:标题栏左侧的图标

    this.requestWindowFeature(Window.FEATURE_LEFT_ICON);
    setContentView(R.layout.main);  
    getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon);

6.FEATURE_NO_TITLE:无标题

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

7.FEATURE_OPTIONS_PANEL:启用“选项面板”功能,默认已启用。
8.FEATURE_PROGRESS:进度指示器功能

    requestWindowFeature(Window.FEATURE_PROGRESS);  
    setProgressBarVisibility(true);  
    setContentView(R.layout.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;  
                    WindowFeatureDemoActivity.this.myMessageHandler.sendMessage(m);  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
    }).start();

    然后通过 Handler 来处理消息,如下:

    Handler myMessageHandler = new Handler() {  
    // @Override  
    public void handleMessage(Message msg) {  
            // 设置标题栏中前景的一个进度条进度值  
            setProgress(100 * msg.what);  
            // 设置标题栏中后面的一个进度条进度值  
            setSecondaryProgress(100 * msg.what + 10);  
            super.handleMessage(msg);  
        }  
    };  

9.FEATURE_RIGHT_ICON:标题栏右侧的图标