使用自定义标题以编程方式设置主题时出错
当我使用 setTheme
方法设置主题时,出现 android.util.AndroidRuntimeException
。这是我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isCustomScreen = getIntent().getBooleanExtra("isCustomScreen", false);
if (isCustomScreen ) {
setTheme(R.style.CustomTitleTheme);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
}
setContentView(R.layout.sample_layout);
if (isCustomScreen ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.custom_title_bar);
}
}
清单文件中的活动声明:
<activity
android:name=".activities.MenuActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
在使用 isCustomScreen
作为 true 运行此代码时遇到以下异常:
02-17 18:35:54.619: E/AndroidRuntime(5787): Caused by: android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
02-17 18:35:54.619: E/AndroidRuntime(5787): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:183)
02-17 18:35:54.619: E/AndroidRuntime(5787): at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2074)
02-17 18:35:54.619: E/AndroidRuntime(5787): at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:2225)
02-17 18:35:54.619: E/AndroidRuntime(5787): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:194)
02-17 18:35:54.619: E/AndroidRuntime(5787): at android.app.Activity.setContentView(Activity.java:1647)
02-17 18:35:54.619: E/AndroidRuntime(5787): at com.sample.activities.MenuActivity.onCreate(MenuActivity.java:39)
02-17 18:35:54.619: E/AndroidRuntime(5787): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-17 18:35:54.619: E/AndroidRuntime(5787): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-17 18:35:54.619: E/AndroidRuntime(5787): ... 11 more
我已尝试删除主题声明从清单文件并在onCreate
方法中设置主题:
setTheme(android.R.style.Theme_Translucent_NoTitleBar);
但是这样做时,当isCustomScreen
为false时,我没有获得半透明背景的活动
请建议我应该做什么。
更新:添加了自定义主题的详细信息:
<style name="CustomWindowTitleBackground">
<item name="android:background">#000000</item>
</style>
<style name="CustomTitleTheme" parent="Theme">
<item name="android:windowTitleSize">45dip</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
I am getting android.util.AndroidRuntimeException
when i am setting the theme using setTheme
method. here is my code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isCustomScreen = getIntent().getBooleanExtra("isCustomScreen", false);
if (isCustomScreen ) {
setTheme(R.style.CustomTitleTheme);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
}
setContentView(R.layout.sample_layout);
if (isCustomScreen ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.custom_title_bar);
}
}
Declaration of activity in Manifest file:
<activity
android:name=".activities.MenuActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
I am getting following exception while running this code with isCustomScreen
as true:
02-17 18:35:54.619: E/AndroidRuntime(5787): Caused by: android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
02-17 18:35:54.619: E/AndroidRuntime(5787): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:183)
02-17 18:35:54.619: E/AndroidRuntime(5787): at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2074)
02-17 18:35:54.619: E/AndroidRuntime(5787): at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:2225)
02-17 18:35:54.619: E/AndroidRuntime(5787): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:194)
02-17 18:35:54.619: E/AndroidRuntime(5787): at android.app.Activity.setContentView(Activity.java:1647)
02-17 18:35:54.619: E/AndroidRuntime(5787): at com.sample.activities.MenuActivity.onCreate(MenuActivity.java:39)
02-17 18:35:54.619: E/AndroidRuntime(5787): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-17 18:35:54.619: E/AndroidRuntime(5787): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-17 18:35:54.619: E/AndroidRuntime(5787): ... 11 more
I have tried removing the theme declaration from the manifest file and setting the theme in onCreate
method:
setTheme(android.R.style.Theme_Translucent_NoTitleBar);
but on doing this i am not getting the activity with Translucent background when isCustomScreen
as false
Please suggest what should i do.
Update: added details of custom theme:
<style name="CustomWindowTitleBackground">
<item name="android:background">#000000</item>
</style>
<style name="CustomTitleTheme" parent="Theme">
<item name="android:windowTitleSize">45dip</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)