Android自定义标题栏

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);//必须设置在setContentView之前。
   setContentView(R.layout.activity_main);

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);//custom_title为自定义标题栏布局

}

修改AndroidManifest.xml中该Activity的theme为以下:

其中parent要设为android:Theme或android:Theme.Light,不能设为android:Theme.Holo.Light,否则会报You cannot combine custom titles with other title features错误,可能是因为android:Theme.Holo.Light是用于ActionBar的,跟自定义标题栏有冲突。

<style name="mystyle" parent="android:Theme">
	<item name="android:windowTitleSize">50dp</item>//改为跟自定义标题栏的高度一样
 
 
	<item name="android:windowTitleBackgroundStyle">@color/title_bg</item>//改为跟自定义标题栏的背景一样
</ style >

猜你喜欢

转载自blog.csdn.net/mqdxiaoxiao/article/details/49275233