当应用需要一个相同的标题时或者是工具栏时我们可以使用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属性,否则会报错。
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AndroidTestActivity"
android:theme="@style/tudou_titlebar"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
theme属性:
<resources>
<style name="tudou_titlebar" parent="@android:style/Theme">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowTitleSize">40dp</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
<style name="CustomWindowTitleBackground">
<item name="android:background">@android:color/darker_gray</item>
</style>
</resources>