---
title: "自定义android标题栏TitleBar"
author: "80demon"
date: "2011-11-16T15:09:36Z"
tags: []
categories: ["移动开发"]
slug: "android-titlebar"
draft: false
lastmod: "2011-11-16T15:09:36Z"
id: 1262058
url: "https://80demon.iteye.com/blog/1262058"
---

很多网友发现自己Android程序的标题栏TitleBar区域很单调，如果想个性化一些可以通过下面的方法来为自己软件的标题定制一个layout布局文件，比如浏览器的标题栏，它包含了网站的Favicon，自定义的进度条，和不确定的进度指示等等，实现的方法自己控制吧。下面代码在onCreate中使用，同时顺序不要改变，否则将无法生效:

Java代码   
1.requestWindowFeature(Window.FEATURE\_CUSTOM\_TITLE);       
2.setContentView(R.layout.main);   //软件activity的布局    
3.getWindow().setFeatureInt(Window.FEATURE\_CUSTOM\_TITLE, R.layout.titlebar);  //titlebar为自己标题栏的布局   
requestWindowFeature(Window.FEATURE\_CUSTOM\_TITLE);    
setContentView(R.layout.main);   //软件activity的布局  
getWindow().setFeatureInt(Window.FEATURE\_CUSTOM\_TITLE, R.layout.titlebar);  //titlebar为自己标题栏的布局

这样虽然可以在一定程度上定制标题栏, 不过, 这里无法改变标题栏的高度和背景(背景设置之后会在两端有两个非常难看的边框).  据说, 原因是android 固有的.  
这里有修改方法:  
原理是这样的. 直接像上述代码那样添加title仅仅是把一个子界面添加到原有的title上的, 并没有改变原来的属性, 比如 标题栏大小, 标题栏背景. 这些需要在theme 主题里面定义.  
因此先定义一个style, 若修改背景请修改android:windowTitleBackgroundStyle  
若修改标题栏高度,请修改android:windowTitleSize  
例子:  
Xml代码   
1.&lt;?xml version="1.0" encoding="utf-8"?&gt;   
2.&lt;resources xmlns:android="http://schemas.android.com/apk/res/android"&gt;   
3.   
4.   
5.&lt;style name="CustomWindowTitleBackground"&gt;   
6.       &lt;item name="android:background"&gt;#565656&lt;/item&gt;   
7.&lt;/style&gt;   
8.   
9.   
10.&lt;style name="test" parent="android:Theme"&gt;   
11.     &lt;item name="android:windowTitleSize"&gt;50dp&lt;/item&gt;   
12.     &lt;item name="android:windowTitleBackgroundStyle"&gt;@style/CustomWindowTitleBackground&lt;/item&gt;   
13.&lt;/style&gt;   
14.&lt;/resources&gt;   
&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;resources xmlns:android="http://schemas.android.com/apk/res/android"&gt;

&lt;style name="CustomWindowTitleBackground"&gt;  
       &lt;item name="android:background"&gt;#565656&lt;/item&gt;  
&lt;/style&gt;

&lt;style name="test" parent="android:Theme"&gt;  
     &lt;item name="android:windowTitleSize"&gt;50dp&lt;/item&gt;  
     &lt;item name="android:windowTitleBackgroundStyle"&gt;@style/CustomWindowTitleBackground&lt;/item&gt;  
&lt;/style&gt;  
&lt;/resources&gt;

在程序的android manifest.xml中对应activity中添加属性  android:theme = "@style/test"  就可以了  
Xml代码   
1.&lt;?xml version="1.0" encoding="utf-8"?&gt;   
2.&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"   
3.      package="com.guardian"   
4.      android:versionCode="1"   
5.      android:versionName="1.0"&gt;   
6.    &lt;application android:icon="@drawable/icon" android:label="@string/app\_name" &gt;   
7.        &lt;activity android:name=".Guardian"   
8.                  android:label="@string/app\_name"   
9.                  android:theme = "@style/test"   //就在这里    
10.                  &gt;   
11.            &lt;intent-filter&gt;   
12.                &lt;action android:name="android.intent.action.MAIN" /&gt;   
13.                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;   
14.            &lt;/intent-filter&gt;   
15.        &lt;/activity&gt;   
16.   
17.    &lt;/application&gt;   
18.    &lt;uses-sdk android:minSdkVersion="4" /&gt;   
19.   
20.&lt;/manifest&gt;    
&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"  
      package="com.guardian"  
      android:versionCode="1"  
      android:versionName="1.0"&gt;  
    &lt;application android:icon="@drawable/icon" android:label="@string/app\_name" &gt;  
        &lt;activity android:name=".Guardian"  
                  android:label="@string/app\_name"  
                  android:theme = "@style/test"   //就在这里  
                  &gt;  
            &lt;intent-filter&gt;  
                &lt;action android:name="android.intent.action.MAIN" /&gt;  
                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;  
            &lt;/intent-filter&gt;  
        &lt;/activity&gt;

    &lt;/application&gt;  
    &lt;uses-sdk android:minSdkVersion="4" /&gt;

&lt;/manifest&gt;

之后借助于设置自定义的标题栏xml文件,就可以自定义标题栏布局了