0

请看下面的代码

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

public class Home extends Activity {

    private TextView textView23;

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

         //Removing the title bar
         this.requestWindowFeature(Window.FEATURE_NO_TITLE);


         setContentView(R.layout.activity_home);

        //Setting up the Action Bar
         ActionBar actionBar = getActionBar();  //to support lower version too
         actionBar.setDisplayShowCustomEnabled(true);
         View customView=getLayoutInflater().inflate(R.layout.window_title, null);
         actionBar.setCustomView(customView);
         actionBar.setBackgroundDrawable(new ColorDrawable(Color.WHITE));



        textView23 = (TextView)findViewById(R.id.textView23);
        textView23.setSelected(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

}

我在这里删除应用程序的标题栏。但是,一旦执行此代码,我就会收到以下错误。

10-30 08:09:58.480: E/AndroidRuntime(2747): FATAL EXCEPTION: main
10-30 08:09:58.480: E/AndroidRuntime(2747): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xx.xx/com.xx.xx.Home}: java.lang.NullPointerException
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.os.Looper.loop(Looper.java:137)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at java.lang.reflect.Method.invokeNative(Native Method)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at java.lang.reflect.Method.invoke(Method.java:511)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at dalvik.system.NativeStart.main(Native Method)
10-30 08:09:58.480: E/AndroidRuntime(2747): Caused by: java.lang.NullPointerException
10-30 08:09:58.480: E/AndroidRuntime(2747):     at com.xx.xx.Home.onCreate(Home.java:29)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.Activity.performCreate(Activity.java:5104)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-30 08:09:58.480: E/AndroidRuntime(2747):     ... 11 more

由于以下输出,我正在尝试这样做

在此处输入图像描述

如您所见,应用程序标题和我的操作栏都在显示。我不想要应用程序标题或标题图像。

4

3 回答 3

2

没有 Title 就不能使用 ActionBar。所以删除线

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

您的主要目的是从标题栏中删除标题和图标,然后使用下面的代码

ActionBar actionBar = getActionBar();  //to support lower version too
actionBar.setDisplayShowHomeEnabled(false); // Remove icon
actionBar.setDisplayShowTitleEnabled(false); // Remove title text
actionBar.setDisplayShowCustomEnabled(true);
View customView=getLayoutInflater().inflate(R.layout.window_title, null);
actionBar.setCustomView(customView);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
于 2013-10-30T09:05:42.893 回答
0

试试这个:this.requestWindowFeature(getWindow().FEATURE_NO_TITLE); 这对我有用。

也试试这个:在你的 AndroidManifest<activity>标签中,添加:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
于 2013-10-30T09:01:24.543 回答
0

在清单中,您可以使用android:theme="@android:style/Theme.NoTitleBar.Fullscreen"标签<activity>的主题

把它getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);放在你的java类中。

这将删除默认的应用程序名称和图标。

然后您可以使用 ActionBar 来设置应用程序名称。

于 2013-10-30T09:06:17.957 回答