尝试使用Window.FEATURE_CUSTOM_TITLE,但得到例外:您无法将自定义标题与其他标题功能组合在一起.

xue*_*eru 16 android

我正在尝试使用自定义标题在标题栏中包含图像按钮.我在这篇文章中得到了很多帮助:android:在应用程序的标题中添加按钮?,但无法使其适用于我的ListActivity.

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

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

当我运行应用程序时,我得到了AndroidRuntimeException:您无法将自定义标题与其他标题功能组合在一起.

基于堆栈跟踪,com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:183)抛出异常,该异常由setlistAdapter调用触发.

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

提前致谢.

Mac*_*rse 19

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

<item name="android:windowNoTitle">true</item>
Run Code Online (Sandbox Code Playgroud)

从我的 theme.xml

  • 这为我修好了,但是有一个值得注意的Flash,其中标题栏仍然会出现一秒钟然后隐藏.有什么方法吗? (3认同)

小智 10

使您在"值"文件夹中创建自定义样式.确保编码如下.

<style name="CustomTheme" parent="android:Theme"> 
Run Code Online (Sandbox Code Playgroud)

不要修改父参数.

这确实对我有用.


use*_*184 5

您也可以:而不是修改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>
Run Code Online (Sandbox Code Playgroud)

您可以在此主题中定义其他设置.

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

 android:theme="@style/MyTheme" 
Run Code Online (Sandbox Code Playgroud)

最后在您的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);

}
Run Code Online (Sandbox Code Playgroud)

不要忘记在布局文件夹中定义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>
Run Code Online (Sandbox Code Playgroud)