【问题标题】:Try to use Window.FEATURE_CUSTOM_TITLE but got Exception:You cannot combine custom titles with other title feature..尝试使用 Window.FEATURE_CUSTOM_TITLE 但出现异常:您不能将自定义标题与其他标题功能结合使用..
【发布时间】:2010-04-21 21:04:45
【问题描述】:

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

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

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

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

公共类 WorkOrderListActivity 扩展 ListActivity { String[] 订单={"WO-12022009", "WO-12302009","WO-02122010", "02152010"}; @覆盖 公共无效 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)); } }

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

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

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

提前致谢。

【问题讨论】:

    标签: android


    【解决方案1】:

    我遇到了同样的问题,我修复它删除

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

    来自我的theme.xml

    【讨论】:

    • 这为我解决了这个问题,但是有一个明显的 Flash,标题栏仍然出现一秒钟然后隐藏。有什么办法吗?
    • 嗨,我知道这是一篇旧帖子,但你能帮我找到 theme.xml 文件吗?它不是我项目中的默认 xml 文件
    • 我也没有theme.xml :(
    • @Oliv:你需要创建它。
    • 我有一个styles.xml,由向导创建,是一样的。
    【解决方案2】:

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

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

    不要修改父参数。

    这确实对我有用。

    【讨论】:

    • 这很奇怪。非常f@@@ing 无证业务。
    【解决方案3】:

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

    在 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>
    

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

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

     android:theme="@style/MyTheme" 
    

    最后像往常一样在你的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);
    
    }
    

    不要忘记在布局文件夹中定义 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>
    

    【讨论】:

      【解决方案4】:

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

          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);
          }
      

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

          我所做的与 Sunny Dasari 所做的完全一样,但做了一点小改动,我将 @ 放在前面,并将 android 放在父属性中。

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

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

          【讨论】:

            【解决方案7】:

            为避免崩溃,您可以简单地添加

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

            到您的 AndroidManifest.xml 中的 &lt;Activity&gt; 标签:

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

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

            但是,您可能还需要定义自己的主题来更改其他属性,例如 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>
            

            【讨论】:

            • 天哪!为什么你的答案不是第一?为什么它有 0 票。你完全揭开了这个问题的神秘面纱。我什至使用了 try catch 块,但仍然崩溃了! Android 是我编写过的最糟糕的平台。困难是游戏的名称。如果您想使用谷歌的免费操作系统,那么欢迎来到痛苦的世界。
            • 还有你怎么知道主题的属性。您使用哪个参考网站?
            【解决方案8】:

            尝试交换以下行:

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

            【讨论】:

            • 我遇到了同样的问题,这并没有解决强制关闭问题。
            【解决方案9】:

            我也遇到了这个问题,看起来这是一个问题,是什么主题应用于 AndroidManifest.xml 文件中的活动。如果我使用这样的主题:

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

            然后它会抛出错误

            android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
            

            但是,如果我使用不同的主题,例如:

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

            那么它就不会抛出错误,随后也不会崩溃。但是我正在尝试使用像Theme.Holo 这样的主题。我不确定是否有办法解决这个问题。

            【讨论】:

              【解决方案10】:

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

              【讨论】:

                【解决方案11】:

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

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

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

                如下所示

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

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

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

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多