---
title: "Android 应用程序窗体显示状态操作（requestWindowFeature()的应用）"
author: "jguangyou"
date: "2012-02-29T10:19:51Z"
tags: []
categories: ["移动开发"]
slug: "android-requestwindowfeature"
draft: false
lastmod: "2012-02-29T10:19:51Z"
id: 1431514
url: "https://deep-fish.iteye.com/blog/1431514"
---

我们在开发程序是经常会需要软件全屏显示、自定义标题（使用按钮等控件）和其他的需求，今天这一讲就是如何控制Android应用程序的窗体显示.

　　首先介绍一个重要方法那就是requestWindowFeature(featrueId),它的功能是启用窗体的扩展特性。参数是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:标题栏右侧的图标

 

二、详解

默认显示状态

![](http://pic002.cnblogs.com/images/2010/133128/2010113015050759.png)

图1默认

1.FEATURE\_CUSTOM\_TITLE详解

this.requestWindowFeature(Window.FEATURE\_CUSTOM\_TITLE);  
setContentView(R.layout.main);

 

![](http://pic002.cnblogs.com/images/2010/133128/2010113015054790.png)

图2 无标题

 

这是因为没设置Featrue

在上面代码后加：getWindow().setFeatureInt(Window.FEATURE\_CUSTOM\_TITLE, R.layout.title);　

![](http://pic002.cnblogs.com/images/2010/133128/2010113015173031.png)　

图3自定义标题

自定义标题完成，它是一个xml文件布局

title.xml

**&lt;?xml version="1.0" encoding="utf-8"?&gt;**  
**&lt;LinearLayout**  
**xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"**  
**android:layout\_width="wrap\_content"**  
**android:layout\_height="wrap\_content" &gt;**

**&lt;ImageView android:layout\_width="wrap\_content"**  
**android:layout\_height="wrap\_content"**  
**android:src="@drawable/icon"/&gt;**  
**&lt;TextView android:id="@+id/text"**  
**android:layout\_width="wrap\_content"**  
**android:layout\_height="wrap\_content"**  
**android:layout\_alignParentLeft="true"**  
**android:text="文本" /&gt;**

**&lt;/LinearLayout&gt;**

3.FEATURE\_INDETERMINATE\_PROGRESS详解

表示一个进程正在运行

![](http://pic002.cnblogs.com/images/2010/133128/2010113015283616.png)

　　　　图4标题进度条显示

实现代码

**1.progress.xml**

**&lt;?xml version="1.0" encoding="utf-8"?&gt;**  
**&lt;LinearLayout**  
**xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"**  
**android:layout\_width="wrap\_content"**  
**android:layout\_height="wrap\_content"&gt;**  
**&lt;ProgressBar android:id="@+id/progress"**

**android:layout\_width="wrap\_content"**

**android:layout\_height="wrap\_content"**

**android:layout\_gravity="center\_vertical"**  
**style="?android:attr/progressBarStyleSmallTitle"&gt;**

**&lt;/ProgressBar&gt;**  
**&lt;/LinearLayout&gt;**

 

2.**Java代码**

**this.requestWindowFeature(Window.FEATURE\_INDETERMINATE\_PROGRESS);**  
**setContentView(R.layout.main);**

**getWindow().setFeatureInt(Window.FEATURE\_INDETERMINATE\_PROGRESS, R.layout.progress);**  
**setProgressBarIndeterminateVisibility(true);**

3.FEATURE\_LEFT\_ICON详解

左侧显示图标

![](http://pic002.cnblogs.com/images/2010/133128/2010113015431411.png)

图5

 

实现代码

this.requestWindowFeature(Window.FEATURE\_LEFT\_ICON);  
setContentView(R.layout.main);

getWindow().setFeatureDrawableResource(Window.FEATURE\_LEFT\_ICON, R.drawable.icon);

 

4.FEATURE\_NO\_TITLE详解

可用于全屏显示

 

实现代码

**this.requestWindowFeature(Window.FEATURE\_NO\_TITLE);**  
**setContentView(R.layout.main);**

**getWindow().setFlags(WindowManager.LayoutParams.FLAG\_FULLSCREEN, WindowManager.LayoutParams.FLAG\_FULLSCREEN);**

![](http://pic002.cnblogs.com/images/2010/133128/2010113015530648.png)

图6全屏显示