如何在Android中删除默认操作栏并添加工具栏?

vis*_*was 0 android android-actionbar android-toolbar

我正在开发一个具有窗口默认操作栏的应用程序,但是我想删除该应用程序栏并添加工具栏。我该如何添加

删除操作栏的代码:

 <activity
        android:name=".CMainActivity"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"
       />
Run Code Online (Sandbox Code Playgroud)

我正在扩展AppCompatActivity

// navigation bar code
    m_Drawer = (DrawerLayout) findViewById(R.id.drawer_layout);//finding id of drawerlayout
    s_drawerToggle = new ActionBarDrawerToggle(
            this, m_Drawer, m_Toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    m_Drawer.setDrawerListener(s_drawerToggle);
    m_Drawer.setScrimColor(getResources().getColor(android.R.color.transparent));
    s_drawerToggle.syncState();

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<LinearLayout
    android:id="@+id/container_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"

        android:titleTextColor="#FFFF"
        app:layout_scrollFlags="scroll|enterAlways"
        tools:ignore="UnusedAttribute">
    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        android:minHeight="?attr/actionBarSize"
        app:tabIndicatorColor="@android:color/white"
        app:tabIndicatorHeight="2dp"
        app:tabTextAppearance="?android:textAppearanceMedium"
        tools:ignore="UnusedAttribute"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer"
    tools:ignore="PrivateResource"/>
Run Code Online (Sandbox Code Playgroud)

El.*_*El. 5

在您的styles.xml中,将AppTheme设置为NoActionBar,如下所示-

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Run Code Online (Sandbox Code Playgroud)

然后在你的活动中

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Run Code Online (Sandbox Code Playgroud)

并将此工具栏添加到您的layout.xml文件中,如下所示-

 <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_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@color/primaryColor" />
Run Code Online (Sandbox Code Playgroud)