开发者

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

开发者 https://www.devze.com 2022-12-27 22:12 出处:网络
I am trying to use a custom titleto 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 Lis

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.

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:

    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.

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.

Does anyone have the same problem wi开发者_如何学Goth ListActivity? Also once I manage to get this work, how do I attach listeners to the image button for it to do something?

Thanks in advance.


I had the same issue and I fix it deleting

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

from my theme.xml


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

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

Don't modify parent parameter.

This did work for me.


Instead of modifying your theme.xml you may also:

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

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

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

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


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


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.

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


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

So my code looked like this.

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


To avoid crashing, you can simply add

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

to the <Activity> tag in your 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.

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:

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


Try swapping following lines:

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


I have run into this issue as well and it looks like it is an issue with what theme is applied to an activity in the AndroidManifest.xml file. If I use a theme like:

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

Then it will throw the error

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

However if I use a different theme like:

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

then it will not throw the error and subsequently will not crash. However I am trying to use a theme like Theme.Holo. I'm not sure if there is a way around this.


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.


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)

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

精彩评论

暂无评论...
验证码 换一张
取 消