1

我正在尝试为我的应用程序创建一个自定义标题栏。问题是,如果我进行这些更改,它会在启动时崩溃:

    this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.main);
    this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);

问题可能是什么?我搜索了论坛和网络,但找不到答案。谢谢。

4

2 回答 2

0

设置标题栏后尝试调用setContentView(...)

this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);    
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
setContentView(R.layout.main);
于 2012-08-13T18:31:24.253 回答
0

这对我有用。在我看来,您的 XML 文件中有错误

 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.main);      

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, 
                              R.layout.window_main_title);
 }

XML layout.window_main_title :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#323331">

    <ImageView
        android:id="@+id/header"
        android:src="@drawable/ic_launcher_32"
        android:paddingLeft="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"/>

    <TextView
                android:id="@+id/textHeader"
                android:textSize="15dp"  
                android:textColor="#FEFEFE"
                android:text="@string/app_name"
                android:paddingLeft="5dp"
                android:layout_gravity="center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"></TextView> 


</LinearLayout>
于 2012-08-13T19:30:10.110 回答