参考网址:
http://zhanhao.iteye.com/blog/1174914
http://www.cnblogs.com/salam/archive/2010/11/30/1892143.html
requestWindowFeature可以设置的值有:
<span style="font-family:Microsoft YaHei;">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:标题栏右侧的图标</span>
1.默认显示状态
![](https://i-blog.csdnimg.cn/blog_migrate/9c8daafb2c40fcefc9410973c06bd061.png)
2.FEATURE_CUSTOM_TITLE 自定义标题
![](https://i-blog.csdnimg.cn/blog_migrate/c261a2fe031722de73224e43797286d6.png)
<span style="font-family:Microsoft YaHei;">this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
// 不加上这一行,标题为空白
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);</span>
title.xml:
<span style="font-family:Microsoft YaHei;"><?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></span>
3.FEATURE_INDETERMINATE_PROGRESS 进度条
![](https://i-blog.csdnimg.cn/blog_migrate/055414ffc831e7bf0157ec46b52606d2.png)
progress.xml:
<span style="font-family:Microsoft YaHei;"><?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></span>
<span style="font-family:Microsoft YaHei;">this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, R.layout.progress);
setProgressBarIndeterminateVisibility(true);</span>
3.FEATURE_LEFT_ICON 标题栏左侧的图标
![](https://i-blog.csdnimg.cn/blog_migrate/8fa5a94e380f4635706b607284ae179e.png)
<span style="font-family:Microsoft YaHei;">this.requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.main);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon);</span>
4.FEATURE_NO_TITLE 无标题
可用于全屏显示,代码:
<span style="font-family:Microsoft YaHei;">this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);</span>
虽然上面这样可以在标题栏加入一些控件,但是仍然不能改变标题栏的高度、背景色,要想达到这个目的,只能使用theme(主题)。因此往project里先添加一个style。改变背景色修改android:windowTitleBackgroundStyle的值,改变标题栏高度则修改android:windowTitleSize的值。下面是一个示例:
<span style="font-family:Microsoft YaHei;"><prename="code"class="html">xmlversion="1.0"encoding="utf-8"?>
<resources>
<stylename="CustomWindowTitleBackground">
<itemname="android:background">#778899<item>
<style>
<stylename="activityTitlebar"parent="android:Theme">
<itemname="android:windowTitleSize">32dp<item>
<itemname="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground<item>
<style>
<resources></span>
接着再修改AndroidManifest.xml文件,找到要自定义标题栏的Activity,添加上android:theme值,比如:
<span style="font-family:Microsoft YaHei;"><activity android:name=".MainActivity" android:theme="@style/activityTitlebar"></span>
android:theme值就是上面那个style.xml文件里定义的一个style的name值。按照以上的步骤,修改标题栏布局、高度、背景色的功能就实现了。
另附一个总的Demo:
效果图:
default:
progress:
no title:
lefticon:
fullscreen:
indeterminate_progress:
customtitle:
package com.my;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;
public class WindowFeatureDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
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.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.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitle);
}
private void showFEATURE_LEFT_ICON()
{
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.main);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
R.drawable.icon);
}
private void showFEATURE_NO_TITLE()
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.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.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 myMessageHandler = new Handler() {
// @Override
public void handleMessage(Message msg) {
// 设置标题栏中前景的一个进度条进度值
setProgress(100 * msg.what);
// 设置标题栏中后面的一个进度条进度值
setSecondaryProgress(100 * msg.what + 10);
super.handleMessage(msg);
}
};
}