1.首先要将app原先的toolbar去掉。
要进入到res/layout/style.xml文件里面将AndroidManifest.xml里面用到的Android:theme属性指定的AppTheme的主题修改一下。
即:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resource>
中的
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
修改为:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
这就去掉了原来的toolbar了
2.用ToolBar代替原来的ToolBar
在activity_main.xml里面添加(带有下划线的是要添加的)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.mydroid.MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> </RelativeLayout>
并且在MainActivity.class里面添加代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
3.至此我们已经完成了。