简体   繁体   English

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

[英]How to set title in Navigation Drawer?

I want to set title in the navigation drawer.我想在导航抽屉中设置标题。 in my application i create a navigation drawer it is working but when i am trying to set title the title name is not displaying in the title bar don't know why?在我的应用程序中,我创建了一个导航抽屉,它正在工作,但是当我尝试设置标题时,标题名称没有显示在标题栏中,不知道为什么?

i tried many tutorials but none are working don't know where's the mistake is.我尝试了很多教程,但都没有奏效,不知道错误在哪里。

I tried this way to in other Activity like :-我在其他活动中尝试过这种方式,例如:-

  getActivity().setTitle("Home");

but not working.. here i post my complete code :-但不起作用..在这里我发布了我的完整代码:-

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();


    }
}

Activitymain.xml活动主.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>

Toolbar.xml工具栏.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" />

Menu->drawer.xml菜单->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>

Here i posted the complete code of navigation drawer please tell me how can i set the title name on title bar.我在这里发布了导航抽屉的完整代码,请告诉我如何在标题栏上设置标题名称。

Try below code, hope it could solve your problem:试试下面的代码,希望它能解决你的问题:

1 - In your xml file, put this code 1 - 在您的 xml 文件中,放置此代码

<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=""/>

2 - In your code, put below code 2 - 在你的代码中,把下面的代码

public static Toolbar mToolbar;
mToolbar = (Toolbar) findViewById(R.id.main_toolbar);

3 - Now when you open any fragment from your navigationDrawer, set title in your toolbar as below 3 - 现在,当您从导航抽屉中打开任何片段时,在工具栏中设置标题如下

@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;
     }
}
Try this code in your activity class

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Home");


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

Try this:尝试这个:

<android.support.v7.widget.Toolbar
android:id="@+id/toolbarId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">

 <TextView
    android:id="@+id/toolbarTextId"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:textSize="14sp" 
    android:text="MyToolBar"
  />
</android.support.v7.widget.Toolbar>

你可以试试

getActivity().getSupportActionBar().setTitle("Home");

Can you try this code?你能试试这个代码吗?

setSupportActionbar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setTitle("title");

Below Code Resolved title issue:下面的代码解决了标题问题:

 getSupportActionBar().setDisplayShowTitleEnabled(true);
 getSupportActionBar().setTitle("Title");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM