Android设置自定义标题栏错误

时间:2013-02-23 23:11:48

标签: android

我正在创建一个简单的Android应用,我想在其中添加自定义标题栏。但是,当我创建自定义标题栏时,我遇到了一个问题。

这是我在GetPhoto Activity类中的OnCreate方法:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        boolean titled = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE
        setContentView(R.layout.getphoto);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_complex);

        initialize();
    }

我的title_complex.xml(用于定义自定义标题):

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

    <Imageview
        android:src="@drawable/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:text="Hello world!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#7cfc00" />

</LinearLayout>

当我在setContentView()之后放入getWindow()。setFeatureInt()时,我得到这样的错误:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.featureselection/com.example.featureselection.GetPhoto}: android.view.InflateException: Binary XML file line #16: Error inflating class Imageview
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
...

featureselection是应用程序名称,GetPhoto是包含上面显示的OnCreate方法的类。 我不知道它为什么会出错。我看到其他人也写这样的代码,它工作正常。

另一件事是如果我在setContentView()之前放入getWindow()。setFeatureInt(),当我运行我的应用程序时,它不会出错,但是自定义标题栏上没有显示任何内容,而我希望有一个图像和“hello world”将显示在自定义标题栏上。

供参考: 我在res / values文件夹中也有这个customer_style.xml:

<resources>
    <style name="CustomWindowTitleBackground">
        <item name="android:background">#008000</item>
    </style>

    <style name="CustomTheme" parent="android:Theme">
        <item name="android:windowTitleSize">35dip</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>
</resources>

设置样式。

有谁知道问题是什么以及如何解决?任何分段或解决方案都将被批准。

1 个答案:

答案 0 :(得分:0)

确保您的XML文件使用 ImageView 而不是Imageview - 元素名称区分大小写。

相关问题