如何在导航抽屉中设置标题?

0 android fragment navigation-drawer

我想在导航抽屉中设置标题。在我的应用程序中,我创建了一个导航抽屉,它可以正常工作,但是当我尝试设置标题时,标题名称未显示在标题栏中,不知道为什么?

我尝试了许多教程,但是都没有用,不知道错误在哪里。

我在其他活动中尝试过这种方式:-

  getActivity().setTitle("Home");
Run Code Online (Sandbox Code Playgroud)

但不起作用..在这里,我发布了完整的代码:-

public class MainActivity extends AppCompatActivity {
    DrawerLayout mDrawerLayout;
    NavigationView mNavigationView;
    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;
    SessionManager session;

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

        requestWindowFeature(Window.FEATURE_NO_TITLE);//will hide the title not the title bar
        setContentView(R.layout.activity_main);

        session = new SessionManager(getApplicationContext());
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        mNavigationView = (NavigationView) findViewById(R.id.shitstuff);

        //*//**
        // * Lets inflate the very first fragment
        // * Here , we are inflating the TabFragment as the first Fragment
        // *//*


        //Intent intent = new Intent(getApplicationContext(),MainActivity.class);
        //startActivity(intent);
        mFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();

        // * Setup click events on the Navigation View Items.
        //*

        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                mDrawerLayout.closeDrawers();


                if (menuItem.getItemId() == R.id.nav_home) {
                    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();
                }


               return false;
            }

        });


        //Setup Drawer Toggle of the Toolbar
        android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
        ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name,
                R.string.app_name);

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        mDrawerToggle.syncState();


    }
}
Run Code Online (Sandbox Code Playgroud)

Activitymain.xml

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        android:id="@+id/toolbar"
        app:titleTextColor="@color/white" />

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/drawerLayout"
        >

        <FrameLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/containerView">
        </FrameLayout>

        <android.support.design.widget.NavigationView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:id="@+id/shitstuff"
            app:itemTextColor="@color/black"
            app:menu="@menu/drawermenu"
            android:layout_marginTop="-01dp"
            />

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Toolbar.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
Run Code Online (Sandbox Code Playgroud)

菜单-> drawer.xml

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_menu_camera"
            android:title="HOME" />
        <item android:title="My Report">
            <menu>
                <item
                    android:id="@+id/myvisit"
                    android:icon="@drawable/ic_menu_share"
                    android:title="My Visit" />
                <item
                    android:id="@+id/myorder"
                    android:icon="@drawable/ic_menu_send"
                    android:title="My Order" />
                <item
                    android:id="@+id/mypaymnet"
                    android:icon="@drawable/ic_menu_share"
                    android:title="My Paymnet" />
                <item
                    android:id="@+id/mydelivery"
                    android:icon="@drawable/ic_menu_send"
                    android:title="My Delivery" />
            </menu>
        </item>

        <item android:title="WOD">
            <menu>
                <item
                    android:id="@+id/add"
                    android:icon="@drawable/ic_menu_share"
                    android:title="ADD" />
                <item
                    android:id="@+id/View"
                    android:icon="@drawable/ic_menu_send"
                    android:title="View" />
            </menu>
        </item>

        <item android:title="My Task">
            <menu>
                <item
                    android:id="@+id/DailyTask"
                    android:icon="@drawable/ic_menu_share"
                    android:title="Daily Task" />
                <item
                    android:id="@+id/Scheduletask"
                    android:icon="@drawable/ic_menu_send"
                    android:title="Schedule Task" />
                <item
                    android:id="@+id/onetimetask"
                    android:icon="@drawable/ic_menu_send"
                    android:title="One Time Task" />
            </menu>
        </item>

        <item
            android:id="@+id/myattendence"
            android:icon="@drawable/ic_menu_share"
            android:title="My Attendence" />
        <item
            android:id="@+id/lapp"
            android:icon="@drawable/ic_menu_send"
            android:title="Leave Application" />
        <item
            android:id="@+id/profile"
            android:icon="@drawable/ic_menu_share"
            android:title="Profile" />
        <item
            android:id="@+id/chngpswd"
            android:icon="@drawable/ic_menu_send"
            android:title="Change Password" />
        <item
            android:id="@+id/logout"
            android:icon="@drawable/ic_menu_send"
            android:title="Log Out" />

    </group>
</menu>
Run Code Online (Sandbox Code Playgroud)

在这里,我发布了导航抽屉的完整代码,请告诉我如何在标题栏上设置标题名称。

Bha*_*nik 5

Try below code, hope it could solve your problem:

1 - In your xml file, put this code

<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:layout_below="@+id/main_header"
        android:background="@color/white"
        android:contentInsetLeft="0dp"
        android:contentInsetStart="0dp"
        android:title=""
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        app:navigationIcon="@mipmap/menu_icon"
        app:title=""/>
Run Code Online (Sandbox Code Playgroud)

2 - In your code, put below code

public static Toolbar mToolbar;
mToolbar = (Toolbar) findViewById(R.id.main_toolbar);
Run Code Online (Sandbox Code Playgroud)

3 - Now when you open any fragment from your navigationDrawer, set title in your toolbar as below

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
    //Checking if the item is in checked state or not, if not make it in checked state
    if (menuItem.isChecked()) menuItem.setChecked(false);
    else menuItem.setChecked(true);

    //Closing drawer on item click
    drawerLayout.closeDrawers();

    //Check to see which item was being clicked and perform appropriate action
    switch (menuItem.getItemId()) {

    case R.id.drawer_new_sale:
            mToolbar.setTitle(getString(R.string.dashboard_new_sale));
            return true;

    case R.id.drawer_recent_sales:
            mToolbar.setTitle(getString(R.string.dashboard_recent_sales));
            return true;
     }
}
Run Code Online (Sandbox Code Playgroud)