Android:自定义标题栏崩溃应用程序

时间:2012-02-12 21:25:35

标签: android titlebar

好的,我正在关注创建自定义标题栏的示例:http://thetransientway.com/?p=100。最终结果应该是这样的:

title bar http://thetransientway.com/tutorials/android/content/customtitlebar_output.png

问题是,当我打开应用程序的那一刻,它就崩溃了。这就是我现在所拥有的:

活动文件:

package u.nav.it;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class UNavitActivity extends Activity {
    /** 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.custom_title);
    }
}

custom_title.xml:

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:background="#005BAA"
    android:layout_height="50dp">

<Button
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Button"
android:id="@+id/button1"
android:layout_alignParentRight="true"
android:layout_marginRight="7dp"  >
</Button>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="20dp"
    android:text="Title Content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@android:color/black" />
</RelativeLayout>

custom_style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

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

    <style name="CustomTheme" parent="android:Theme">
        <item name="android:windowTitleSize">50dp</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>

</resources>

main.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="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="u.nav.it"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".CustomTitlebarActivityActivity" android:theme="@style/CustomTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:2)

您尚未在清单中定义活动UNavitActivity。用 UNavitActivity 替换 CustomTitlebarActivityActivity ,我希望它能够正常工作。

答案 1 :(得分:1)

正如已经说过的那样,你没有定义你的活动 可能你改变了应用程序的名称,而不是按照示例的名称,但你没有改变

android:name=".CustomTitlebarActivityActivity" 
清单文件中的

将其编辑为

android:name=".UNavitActivity"