Android 尝试使用 Window.FEATURE_CUSTOM_TITLE 但得到异常:您不能将自定义标题与其他标题功能结合..

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2686556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:56:34  来源:igfitidea点击:

Try to use Window.FEATURE_CUSTOM_TITLE but got Exception:You cannot combine custom titles with other title feature..

android

提问by xueru

I am trying to use a custom title to include an image button to the title bar. I got a lot of help form this post: android: adding button to the title of the app?, but could not get it work for my ListActivity.

我正在尝试使用自定义标题在标题栏中包含一个图像按钮。我从这篇文章中得到了很多帮助:android: add button to the title of the app? ,但无法使其适用于我的 ListActivity。

In a nutshell, following is what I have:

简而言之,以下是我所拥有的:

  1. I hide the titlebar in the AndroidManifest.xml
  2. The specify a relative layout for the custom title (workorder_list_titlebar.xml)

  3. My Activity Class looks like the following:

  1. 我在 AndroidManifest.xml 中隐藏了标题栏
  2. 为自定义标题指定相对布局 (workorder_list_titlebar.xml)

  3. 我的活动类如下所示:

    public class WorkOrderListActivity extends ListActivity {
     String[] orders={"WO-12022009", "WO-12302009","WO-02122010", "02152010"};
     @Override
     public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);   
       requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
       this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.workorder_list_titlebar);
       setContentView(R.layout.workorder_list);
       setListAdapter(new ArrayAdapter(this,R.layout.workorder_list, R.id.label,orders));    
            }
    }

When I ran the app, I got AndroidRuntimeException: You cannot combine custom titles with other title features.

当我运行应用程序时,我得到了 AndroidRuntimeException: You cannot combine custom titles with other title features。

Base on the stack trace, the exception was thrown by com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:183), that was triggered by setlistAdapter call.

根据堆栈跟踪,异常由 com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:183) 引发,由 setlistAdapter 调用触发。

Does anyone have the same problem with ListActivity? Also once I manage to get this work, how do I attach listeners to the image button for it to do something?

有没有人对 ListActivity 有同样的问题?此外,一旦我设法完成这项工作,我如何将侦听器附加到图像按钮以使其执行某些操作?

Thanks in advance.

提前致谢。

回答by Macarse

I had the same issue and I fix it deleting

我有同样的问题,我修复它删除

<item name="android:windowNoTitle">true</item>

from my theme.xml

从我的 theme.xml

回答by Sunny Dasari

Make you create custom style in “values” folder. Make sure you code as below.

让您在“values”文件夹中创建自定义样式。确保你的代码如下。

<style name="CustomTheme" parent="android:Theme"> 

Don't modify parent parameter.

不要修改父参数。

This did work for me.

这对我有用。

回答by user387184

Instead of modifying your theme.xml you may also:

除了修改您的 theme.xml,您还可以:

create a new XML style file my_theme.xml in values folder like this:

在 values 文件夹中创建一个新的 XML 样式文件 my_theme.xml,如下所示:

<style name="MyWindowTitleBackground">
    <item name="android:background">#444444</item>
</style>

<style name="MyTheme" parent="android:Theme">
    <item name="android:windowTitleBackgroundStyle">@style/MyWindowTitleBackground</item>
</style>

You may define other settings as you like in this theme.

您可以根据需要在此主题中定义其他设置。

Then just use this theme in your manifest within the activity's attributes

然后只需在活动属性的清单中使用此主题

 android:theme="@style/MyTheme" 

Finally set your custom title as always in your activity.java:

最后像往常一样在您的activity.java 中设置您的自定义标题:

final Window window = getWindow();
boolean useTitleFeature = false;
// If the window has a container, then we are not free
// to request window features.
if (window.getContainer() == null) {
    useTitleFeature = window
        .requestFeature(Window.FEATURE_CUSTOM_TITLE);
}
setContentView(R.layout.screen_main);

if (useTitleFeature) {
    window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
        R.layout.custom_title);
    // Set up the custom title

    main_title = (TextView) findViewById(R.id.title_left_text);
    main_title.setText(R.string.app_name);
    main_title = (TextView) findViewById(R.id.title_right_text);
    main_title.setText(R.string.Main_titleInfo);

}

Don't forget to define the custom_title.xml file in your layout folder. For example...

不要忘记在布局文件夹中定义 custom_title.xml 文件。例如...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical" >

    <TextView
        android:id="@+id/title_left_text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:ellipsize="end"
        android:singleLine="true" />

    <TextView
        android:id="@+id/title_right_text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="#fff" />

</RelativeLayout>

回答by Alec B. Plumb

I think notenking is right, that this is a problem in activities within tabs. Since some of my activities can either be stand-alone or within a tab, I've found the following helps:

我认为笔记是正确的,这是标签内活动的问题。由于我的一些活动可以是独立的,也可以是在一个选项卡中,我发现以下帮助:

    final Window window = getWindow();

    boolean useTitleFeature = false;
    // If the window has a container, then we are not free
    // to request window features.
    if(window.getContainer() == null) {
        useTitleFeature = window.requestFeature(Window.FEATURE_CUSTOM_TITLE);
    }

    setContentView(layoutId);

    if (useTitleFeature) {
        window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
    }

回答by Monk

I did exactly as Sunny Dasari did but with one small change I put the @ before and android in the parent attribute.

我和 Sunny Dasari 完全一样,但做了一个小改动,我将 @before 和 android 放在父属性中。

So my code looked like this.

所以我的代码看起来像这样。

<style name="CustomTheme" parent="@android:Theme">

回答by notenking

May be you find this problem when use it in tab,for there already have a title and you can not add a custom title again.

可能你在tab中使用的时候会发现这个问题,因为已经有标题了,不能再添加自定义标题了。

you should add this custom title in the activity which you get the Tab

您应该在获得 Tab 的活动中添加此自定义标题

回答by Danke Xie

To avoid crashing, you can simply add

为避免崩溃,您只需添加

android:theme="@style/android:Theme" 

to the <Activity>tag in your AndroidManifest.xml:

<Activity>您的 AndroidManifest.xml 中的标记:

<activity
    android:name="test.TestActivity"
    android:label="@string/app_name"
    android:theme="@style/android:Theme">

This is because the styles defined in your default theme conflict with FEATURE_CUSTOM_TITLE (such as the attribute android:windowNoTitle). By using another theme, you can avoid such problems.

这是因为在您的默认主题中定义的样式与 FEATURE_CUSTOM_TITLE(例如属性android:windowNoTitle)冲突。通过使用其他主题,您可以避免此类问题。

However, you might further need to define your own theme to change other attributes, such as android:windowTitleSize, background color, text color and font, etc. In this case, you can derive your theme from an existing theme (e.g., Theme.Light) and modify its attributes:

但是,您可能还需要定义自己的主题来更改其他属性,例如 android:windowTitleSize、背景颜色、文本颜色和字体等。在这种情况下,您可以从现有主题(例如 Theme. Light)并修改其属性:

<resources>
    <style name="CustomWindowTitleBackground">
        <item name="android:background">#323331</item>
    </style>

    <style name="CustomTheme" parent="@style/android:Theme.Light">
        <item name="android:windowNoTitle">false</item>
        <item name="android:windowTitleSize">60dip</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>
</resources>

回答by alinux

Try swapping following lines:

尝试交换以下几行:

setContentView(R.layout.workorder_list);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.workorder_list_titlebar);

回答by Prativa

Since, I was trying to compile my program in android 4.0, I was facing the same problem. None of these solutions helped.So, I copied my style contents from values > styles.xml and pasted it in values-v11 styles.xml file and values-v14 styles.xml file. Bingo, the trick worked.

因为,我试图在 android 4.0 中编译我的程序,我面临着同样的问题。这些解决方案都没有帮助。所以,我从 values>styles.xml 复制了我的样式内容,并将其粘贴到 values-v11 样式.xml 文件和 values-v14 样式.xml 文件中。宾果游戏,这招奏效了。

回答by madhu131313

As a beginner most of the answers didn't help me for my case. So here is my answer.

作为初学者,大多数答案对我的情况没有帮助。所以这是我的答案。

Go to res/values folder in your android project and check for strings.xml (this file may vary in your case, something like themes.xml)

转到您的 android 项目中的 res/values 文件夹并检查 strings.xml(此文件可能因您的情况而异,例如 themes.xml)

Inside the file under resource tag check whether you have style tags. If you don't find it, add the code below as mentioned below as a child to resources tag

在资源标签下的文件内检查您是否有样式标签。如果没有找到,请将下面提到的代码作为子项添加到资源标签

something like below

像下面这样的东西

<resources>
    <style name="SomeNameHere">
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>

if you already have style tag, just add the code below to your style tag

如果您已经有样式标签,只需将下面的代码添加到您的样式标签中

    <item name="android:windowNoTitle">true</item>