---
title: "自定义Activity标题栏(Title bar)和窗体显示状态操作（requestWindowFeature()的应用）"
author: "Java-Object"
date: "2013-08-27T17:15:22Z"
tags: []
categories: ["编程语言"]
slug: "activity-title-bar-requestwindowfeature"
draft: false
lastmod: "2013-08-27T17:15:22Z"
id: 1931731
url: "https://hezd.iteye.com/blog/1931731"
---

**1. 标题栏显示图标**

public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE\_LEFT\_ICON);

        setContentView(R.layout.main);  
       
        getWindow().setFeatureDrawableResource(Window.FEATURE\_LEFT\_ICON,  
                        android.R.drawable.icon);  
        // ...  
}

但实际效果呢，我觉得不好看，和旁边的文字有相当距离！看看别人的图片的：

![](http://android.blog.51cto.com/attachment/201108/172944760.png)

 

当然这个图标也可以通过自定义布局，使用ImageView来实现：

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;LinearLayout xmlns: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;

效果图：

![](http://android.blog.51cto.com/attachment/201108/163821751.png)

 

**2.自定义布局**

看看我自定义的标题栏：

![](http://android.blog.51cto.com/attachment/201108/155802165.png)

布局代码（titlebar.xml）

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;LinearLayout  
        xmlns:android="http://schemas.android.com/apk/res/android"  
        android:orientation="horizontal"  
        android:layout\_width="fill\_parent"  
        android:layout\_height="wrap\_content"  
&gt;  
        &lt;TextView  
                android:text="@string/app\_name"  
                android:textColor="#000"  
                android:paddingRight="3.0dip"  
                android:layout\_width="wrap\_content"  
                android:layout\_height="wrap\_content"/&gt;  
        &lt;TextView  
                android:text="@string/battery\_text"  
                android:textColor="#000"  
                android:paddingRight="3.0dip"  
                android:layout\_width="wrap\_content"  
                android:layout\_height="wrap\_content"/&gt;  
        &lt;TextView  
                android:id="@+id/battery\_text"  
                android:textColor="#00f"  
                android:layout\_width="wrap\_content"  
                android:layout\_height="wrap\_content"/&gt;  
&lt;/LinearLayout&gt;

 

Java代码：

public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE\_CUSTOM\_TITLE); 

        setContentView(R.layout.main);

        //自定义标题栏  
        mWindow = getWindow();  
        mWindow.setFeatureInt(Window.FEATURE\_CUSTOM\_TITLE,R.layout.titlebar);

        mBatteryText = (TextView)findViewById(R.id.battery\_text);

        mBatteryInforeceiver = new BroadcastReceiver(){

             @Override  
             public void onReceive(Context context, Intent intent) {  
                     int level = intent.getIntExtra("level", 0);  
                     int scale = intent.getIntExtra("scale", 1);  
                     mBatteryText.setText(String.valueOf((int)(level\*100/scale))+"%");  
             }  
               
        };  
}

你还可以添加其他控件，而这些控件的获取和事件响应都是直接在activity里面完成。

 

**3. 设置标题栏的背景色和高度**

虽然我们可以通过自定义布局文件在标题栏加入一些控件，但是仍然不能改变标题栏的高度、背景色，要想达到这个目的，只能使用theme(主题)。

\\res\\values\\style.xml:

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;resources&gt;  
        &lt;style name="CustomWindowTitleBackground"&gt;  
                &lt;item name="android:background"&gt;#47B2FF&lt;/item&gt;  
        &lt;/style&gt;

        &lt;style name="activityTitlebar" parent="android:Theme"&gt;  
                &lt;item name="android:windowTitleSize"&gt;34dp&lt;/item&gt; &lt;!-- 高度 --&gt;  
                &lt;item name="android:windowTitleBackgroundStyle"&gt;@style/CustomWindowTitleBackground&lt;/item&gt;    &lt;!-- 背景色,需要调用前面的颜色设置 --&gt;  
        &lt;/style&gt;  
&lt;/resources&gt;

 

**窗体显示状态操作（requestWindowFeature()的应用）**

 

首先介绍一个重要方法那就是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:标题栏右侧的图标

 

对于默认启用的和前面有介绍的就略去不提了。我们说比较常用的FEATURE\_INDETERMINATE\_PROGRESS和FEATURE\_NO\_TITLE。

 

**FEATURE\_INDETERMINATE\_PROGRESS：表示一个进程正在运行**

progress.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;LinearLayout xmlns: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;

Java代码

public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);

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

        getWindow().setFeatureInt(Window.FEATURE\_INDETERMINATE\_PROGRESS, R.layout.progress);  
        setProgressBarIndeterminateVisibility(true); //适当时候set false来隐藏

        //...  
}

 

标题进度条显示

![](http://android.blog.51cto.com/attachment/201108/180226404.png)

 

**FEATURE\_NO\_TITLE** 就是不显示标题栏，某些时候全屏需要，但全屏不等于不显示标题栏，我尝试显示标题栏的同时全屏来去掉系统的状态栏：

Java代码

public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE\_CUSTOM\_TITLE); 

        setContentView(R.layout.main);

        //自定义标题栏  
        mWindow = getWindow();  
        mWindow.setFeatureInt(Window.FEATURE\_CUSTOM\_TITLE,R.layout.titlebar);

        /* full screen \*/  
        mWindow.setFlags(WindowManager.LayoutParams.FLAG\_FULLSCREEN,  
                        WindowManager.LayoutParams.FLAG\_FULLSCREEN);

        // ...  
}

所以真正实现全屏的是后面的那句话!

 

效果图

![](http://android.blog.51cto.com/attachment/201108/180836825.png)

 

\*\*\*\*\*\*\*\*\*\**

本文部分内容摘录于《Android 应用程序窗体显示状态操作（requestWindowFeature()的应用）》[http://www.cnblogs.com/salam/archive/2010/11/30/1892143.html](http://www.cnblogs.com/salam/archive/2010/11/30/1892143.html)