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

发布于 2025-01-05 01:29:33 字数 3799 浏览 0 评论 0原文

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

标题栏
(来源:thetransientway.com

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

活动文件:

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>

Ok, I am following this example of creating a custom title bar: http://thetransientway.com/?p=100. the end result should be like this:

title bar
(source: thetransientway.com)

The problem is, the moment I open up the app, it just crashes. This is what I have right now:

Activity file:

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>

Manifest:

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉栀 2025-01-12 01:29:33

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

You haven't defined the activity UNavitActivity in your manifest. Replace CustomTitlebarActivityActivity with UNavitActivity and i hope it should work.

人生百味 2025-01-12 01:29:33

正如已经说过的,你还没有定义你的活动
可能您更改了应用程序的名称,而不是遵循示例的名称,但您没有

android:name=".CustomTitlebarActivityActivity" 

在清单文件中

进行更改,将其编辑为

android:name=".UNavitActivity"

as already said you've not defined your activity
probably you changed the name of the app instead of following the example's one, but you didn't change

android:name=".CustomTitlebarActivityActivity" 

in the manifest file

edit it into

android:name=".UNavitActivity"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文