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

时间:2016-12-15 10:12:31

标签: android fragment navigation-drawer

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

我尝试了很多教程,但没有人正在工作,不知道错误在哪里。

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

  getActivity().setTitle("Home");

但没有工作..在这里我发布我的完整代码: -

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

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

表 - &GT; 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>

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

6 个答案:

答案 0 :(得分:3)

尝试以下代码,希望它可以解决您的问题:

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 - 在您的代码中,放在代码

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

3 - 现在当您从navigationDrawer打开任何片段时,在工具栏中设置标题如下

@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;
     }
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

试试这个:

<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>

答案 3 :(得分:0)

你可以尝试

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

答案 4 :(得分:0)

你能试试这段代码吗?

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

答案 5 :(得分:0)

以下代码已解决的标题问题:

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