Android 4.0后,自定义Title出错
我想自定义一个titlebar,新建Android工程时,target SDK选Android2.2,Compile with选了Android 4.0
java代码如下:
[mw_shl_code=java,true]@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
}[/mw_shl_code]
<ignore_js_op style='font: 14px/21px Tahoma, "Microsoft Yahei", Simsun; color: rgb(68, 68, 68); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; orphans: 2; widows: 2; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;'>
运行时,LogCat报错: AndroidRuntimeException: You cannot combine custom titles with other title feature
<ignore_js_op style='font: 14px/21px Tahoma, "Microsoft Yahei", Simsun; color: rgb(68, 68, 68); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; orphans: 2; widows: 2; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;'>
网上说,去掉AndroidManifest.xml中Activity的android:theme="@android:style/Theme.NoTitleBar.Fullscreen"属性。但我看AndroidManifest.xml 根本没有设这个值
[mw_shl_code=java,true]<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.b.MainActivity"
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>[/mw_shl_code]
有可能是@style/AppTheme里面设了这个值吧?于是我再看values/styles.xml文件
<ignore_js_op>
4.0 即API 14,AppBaseTheme应该在values-v14/style.xml里面的
<ignore_js_op>
于是我再去看android:Theme.Holo.Light.DarkActionBar
<ignore_js_op>
<style name="Theme.Holo.Light.DarkActionBar"> 里面没有关于类似<style name="windowNoTitle">的东西
自己解决:
后来,我直接在 AndroidManifest.xml 中,将android:theme="@style/AppTheme" 直接改成android:theme="@style/android:Theme.Light" (就是不继承API 14中的Theme,用回API 11前的Theme)
由于API-11的<style name="Theme.Holo.Light"> 和 API-14<style name="Theme.Holo.Light.DarkActionBar"> 跟API之前版本的<style name="Theme.Light"> 代码太多不同了,所以就我也不明白到底哪里出了问题
浙公网安备 33010602011771号