【问题标题】:Custom titlebar - Android crash自定义标题栏 - Android 崩溃
【发布时间】:2012-08-13 18:30:29
【问题描述】:

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

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

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

【问题讨论】:

    标签: android crash titlebar


    【解决方案1】:

    在设置标题栏后试试这个调用setContentView(...)

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

    【讨论】:

    • 遗憾的是它仍然是同样的即时崩溃。
    【解决方案2】:

    这对我有用。在我看来,您的 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 布局.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>
    

    【讨论】:

    • 谢谢,但这也不起作用。同样的问题。我将尝试获取 logcat。我在某处读到这可能不适用于 Holo?
    猜你喜欢
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多