---
title: "android activity  自定义标题 工具栏  requestWindowFeature"
author: "liangoogle"
date: "2013-02-04T17:34:50Z"
tags: []
categories: ["移动开发"]
slug: "android-activity-requestwindowfeature"
draft: false
lastmod: "2013-02-04T17:34:50Z"
id: 1788755
url: "https://liangoogle.iteye.com/blog/1788755"
---

当应用需要一个相同的标题时或者是工具栏时我们可以使用requestWindowFeature属性：

public class AndroidTestActivity extends Activity {  
    /\** Called when the activity is first created. \*/  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        requestWindowFeature(Window.FEATURE\_CUSTOM\_TITLE);  
        setContentView(R.layout.main);  
        getWindow().setFeatureInt(Window.FEATURE\_CUSTOM\_TITLE, R.layout.title);  
    }  
}

R.layout.title为标题的布局。

要使用这个属性还需要给Manifest.xml属性中相应的activity设置theme属性，否则会报错。

    &lt;application  
        android:icon="@drawable/ic\_launcher"  
        android:label="@string/app\_name" &gt;  
        &lt;activity  
            android:name=".AndroidTestActivity"  
              android:theme="@style/tudou\_titlebar"  
            android:label="@string/app\_name" &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;

theme属性：

&lt;resources&gt;  
       &lt;style name="tudou\_titlebar" parent="@android:style/Theme"&gt;  
        &lt;item name="android:windowBackground"&gt;@android:color/transparent&lt;/item&gt;  
        &lt;item name="android:windowTitleSize"&gt;40dp&lt;/item&gt;  
        &lt;item name="android:windowTitleBackgroundStyle"&gt;@style/CustomWindowTitleBackground&lt;/item&gt;  
    &lt;/style&gt;  
        &lt;style name="CustomWindowTitleBackground"&gt;  
        &lt;item name="android:background"&gt;@android:color/darker\_gray&lt;/item&gt;  
    &lt;/style&gt;  
&lt;/resources&gt;