【问题标题】:Android setting custom title bar errorAndroid设置自定义标题栏错误
【发布时间】:2013-02-23 23:11:48
【问题描述】:

我正在创建一个简单的 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>

当我将 getWindow().setFeatureInt() 放在 setContentView() 之后时,出现如下错误:

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 方法的类。 我不知道为什么它会出错。我看到其他人也写了这样的代码,并且运行良好。

另一件事是,如果我将 getWindow().setFeatureInt() 放在 setContentView() 之前,当我运行我的应用程序时,它不会给出错误,但自定义标题栏上没有显示任何内容,而我期望图像和“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>

设置样式。

有人知道问题出在哪里以及如何解决吗?将应用任何分段或解决方案。

【问题讨论】:

    标签: android


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-06-07
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多