Android如何将AppCompatActivity的action bar完全替换为自定义action bar?
How to completely replace the action bar of AppCompatActivity with custom action bar in Android?
我绝对是 Android 的初学者。现在我正在学习如何布局 Android 应用程序。所以我开始将导航抽屉与工具栏一起使用。现在我正在创建一个没有默认功能的自定义操作。但它不起作用。
这是我的主要 activity 布局
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
app:menu="@menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>
这是我的工具栏布局
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="?attr/colorPrimaryDark">
</android.support.v7.widget.Toolbar>
这是我的activityclass
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是风格
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
但是当我启动 activity 时,标题和查看菜单项的选项包含在操作栏中,即使我没有像屏幕截图那样在布局中定义。
所以我想做的是用我的自定义工具栏完全替换操作栏,并且我想删除默认行为。
所以我在activity
中尝试用这种方式去掉标题
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("");
setSupportActionBar(toolbar);
但是报错
这是logcat
01-23 12:28:45.945 2972-2972/? D/AndroidRuntime: Shutting down VM
01-23 12:28:45.945 2972-2972/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa614a908)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: FATAL EXCEPTION: main
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.waiyanhein.todo.todo/com.waiyanhein.todo.todo.MainActivity}: java.lang.NullPointerException
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread.access0(ActivityThread.java:141)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: Caused by: java.lang.NullPointerException
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at com.waiyanhein.todo.todo.MainActivity.onCreate(MainActivity.java:21)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5104)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread.access0(ActivityThread.java:141)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
01-23 12:28:45.961 2972-2975/? D/dalvikvm: GC_CONCURRENT freed 249K, 13% free 2610K/2988K, paused 2ms+0ms, total 18ms
01-23 12:28:45.961 386-801/? W/ActivityManager: Force finishing activity com.waiyanhein.todo.todo/.MainActivity
01-23 12:28:46.177 386-801/? D/dalvikvm: GC_FOR_ALLOC freed 1205K, 46% free 8703K/16012K, paused 15ms, total 15ms
01-23 12:28:46.209 386-402/? D/dalvikvm: GC_FOR_ALLOC freed 149K, 40% free 9732K/16012K, paused 19ms, total 19ms
01-23 12:28:46.241 386-402/? D/dalvikvm: GC_FOR_ALLOC freed 6K, 33% free 10832K/16012K, paused 15ms, total 15ms
01-23 12:28:46.241 386-402/? I/dalvikvm-heap: Grow heap (frag case) to 13.126MB for 2536932-byte allocation
01-23 12:28:46.301 386-401/? D/dalvikvm: GC_FOR_ALLOC freed 2K, 29% free 13307K/18492K, paused 59ms, total 59ms
01-23 12:28:46.309 386-389/? D/dalvikvm: GC_CONCURRENT freed <1K, 29% free 13307K/18492K, paused 2ms+1ms, total 10ms
所以我的第一个问题是为什么在 activity 中自定义操作栏时会抛出错误?我的代码有什么问题吗?我的第二个问题是如何完全用我上面问的自定义操作栏替换操作栏。
发生 NullPointerException
是因为您在设置 NoActionBar
主题之前试图访问和修改 Activity
上的支持 ActionBar
Toolbar
作为替补。但是,考虑到您想要的选项菜单行为,您不想这样做。
如果 Activity
具有 ActionBar
实现,其选项菜单将自动创建为 ActionBar
本身的下拉菜单。由于您使用的是 NoActionBar
主题,请不要将 Toolbar
设置为支持 ActionBar
,选项菜单将恢复为旧样式。
我绝对是 Android 的初学者。现在我正在学习如何布局 Android 应用程序。所以我开始将导航抽屉与工具栏一起使用。现在我正在创建一个没有默认功能的自定义操作。但它不起作用。
这是我的主要 activity 布局
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
app:menu="@menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>
这是我的工具栏布局
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="?attr/colorPrimaryDark">
</android.support.v7.widget.Toolbar>
这是我的activityclass
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是风格
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
但是当我启动 activity 时,标题和查看菜单项的选项包含在操作栏中,即使我没有像屏幕截图那样在布局中定义。
所以我想做的是用我的自定义工具栏完全替换操作栏,并且我想删除默认行为。
所以我在activity
中尝试用这种方式去掉标题setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("");
setSupportActionBar(toolbar);
但是报错
这是logcat
01-23 12:28:45.945 2972-2972/? D/AndroidRuntime: Shutting down VM
01-23 12:28:45.945 2972-2972/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa614a908)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: FATAL EXCEPTION: main
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.waiyanhein.todo.todo/com.waiyanhein.todo.todo.MainActivity}: java.lang.NullPointerException
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread.access0(ActivityThread.java:141)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: Caused by: java.lang.NullPointerException
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at com.waiyanhein.todo.todo.MainActivity.onCreate(MainActivity.java:21)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5104)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread.access0(ActivityThread.java:141)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-23 12:28:45.945 2972-2972/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
01-23 12:28:45.961 2972-2975/? D/dalvikvm: GC_CONCURRENT freed 249K, 13% free 2610K/2988K, paused 2ms+0ms, total 18ms
01-23 12:28:45.961 386-801/? W/ActivityManager: Force finishing activity com.waiyanhein.todo.todo/.MainActivity
01-23 12:28:46.177 386-801/? D/dalvikvm: GC_FOR_ALLOC freed 1205K, 46% free 8703K/16012K, paused 15ms, total 15ms
01-23 12:28:46.209 386-402/? D/dalvikvm: GC_FOR_ALLOC freed 149K, 40% free 9732K/16012K, paused 19ms, total 19ms
01-23 12:28:46.241 386-402/? D/dalvikvm: GC_FOR_ALLOC freed 6K, 33% free 10832K/16012K, paused 15ms, total 15ms
01-23 12:28:46.241 386-402/? I/dalvikvm-heap: Grow heap (frag case) to 13.126MB for 2536932-byte allocation
01-23 12:28:46.301 386-401/? D/dalvikvm: GC_FOR_ALLOC freed 2K, 29% free 13307K/18492K, paused 59ms, total 59ms
01-23 12:28:46.309 386-389/? D/dalvikvm: GC_CONCURRENT freed <1K, 29% free 13307K/18492K, paused 2ms+1ms, total 10ms
所以我的第一个问题是为什么在 activity 中自定义操作栏时会抛出错误?我的代码有什么问题吗?我的第二个问题是如何完全用我上面问的自定义操作栏替换操作栏。
发生 NullPointerException
是因为您在设置 NoActionBar
主题之前试图访问和修改 Activity
上的支持 ActionBar
Toolbar
作为替补。但是,考虑到您想要的选项菜单行为,您不想这样做。
如果 Activity
具有 ActionBar
实现,其选项菜单将自动创建为 ActionBar
本身的下拉菜单。由于您使用的是 NoActionBar
主题,请不要将 Toolbar
设置为支持 ActionBar
,选项菜单将恢复为旧样式。