使用自定义标题以编程方式设置主题时出错

发布于 2025-01-07 07:43:28 字数 2983 浏览 1 评论 0原文

当我使用 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);

但是这样做时,当isCustomScreenfalse时,我没有获得半透明背景的活动

请建议我应该做什么。

更新:添加了自定义主题的详细信息:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮茶煮酒煮时光 2025-01-14 07:43:28
<i><application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        //do not write theme setting here 
        android:label="@string/app_name" >
        <activity
            android:name=".activity.loginPageActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" //set theme here
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".activity.HomePageFragmentActivity" >
        </activity>
</application></i>
<i><application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        //do not write theme setting here 
        android:label="@string/app_name" >
        <activity
            android:name=".activity.loginPageActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" //set theme here
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".activity.HomePageFragmentActivity" >
        </activity>
</application></i>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文