初涉android,在自定义标题栏的时候就遇到了一些问题,由于很多都不懂,花了挺多时间才解决。
好了废话不多说,直接上问题:
styles.xml
<style name="AppTheme" parent="android:Theme.Holo"> <style>
我AndroidManifest.xml中使用的正是上述文件中的主题:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > ...... <application>
程序写完,运行时候抛出这样的异常:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.czj.webo/com.czj.webo.MainActivity}:
android.util.AndroidRuntimeException: You cannot combine custom titles with other title features(不能将自定义标题和其他标题功能组合起来) 其意思大概是存在了两个或多个标题栏而导致了冲突吧。
之所以贴出上述的代码,是因为这便是导致程序崩溃的根源。通过各种查资料,发现出现错误的原因是使用了主题“android:Theme.Holo”,将其改为
“android:Theme.Light”便可以了。但是别以为这样就解决问题了,新的问题又来了
自定义的标题栏默认的一些属性值都不是我们自己想要的,所以就需要对自定义的标题栏设置一些基本的属性,通过styles.xml
<style name="CustomWindowTitleBackground"> <item name="android:background">#FFFAFA</item> </style> <style name="test" parent="android:Theme.Light"> <item name="android:windowTitleSize">55dp</item> <item name="android:background">#FFFAFA</item> <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> </style>
然后再AndroidManifest.xml的对应Activity中引用该主题即可。
效果图如下:
原文:http://www.cnblogs.com/johnjer/p/4764764.html