如何在MenuItem中设置图像视图和文本

时间:2015-10-06 06:46:58

标签: android

这是我的菜单项:

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


        <item android:title="">
            <menu>
                <item android:title="" />
                <item android:title="" />
            </menu>
        </item>


        <item
            android:id="@+id/nav_item_inbox"
            android:icon="@drawable/inbox"
            android:title="Inbox" />

        <item
            android:id="@+id/nav_item_sent"
            android:icon="@drawable/sent"
            android:title="Sent" />

        <item
            android:id="@+id/nav_item_draft"
            android:icon="@drawable/draft"
            android:title="Drafts" />


        <item android:title="Others">
            <menu>
                <item
                    android:icon="@drawable/spam"
                    android:title="Spam" />
                <item
                    android:icon="@drawable/bin"
                    android:title="Bin" />
            </menu>
        </item>

        <group android:id="@+id/group_settings_id">

            <item
                android:icon="@drawable/settings"
                android:title="Settings" />
            <item
                android:icon="@drawable/help"
                android:title="Help" />
        </group>


    </menu>

这是我的班级:

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

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            /**
             *Setup the DrawerLayout and NavigationView
             */

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

                 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_item_sent) {
                         FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                         fragmentTransaction.replace(R.id.containerView,new SentFragment()).commit();

                     }


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

                    if (menuItem.getItemId() == R.id.nav_item_inbox) {
                        FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                        xfragmentTransaction.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();

        }
    }

我想在Navigaition菜单项中添加图像和文本视图我无法设置背景颜色和图像,而我必须设置ImageView以显示用户Pic和TextView以显示用户名。

enter image description here

我必须达到这样的目标,请帮助我如何做到这一点,请建议。

1 个答案:

答案 0 :(得分:1)

您可以将标题布局添加到XML中的NavigationView

<android.support.design.widget.NavigationView
            android:id="@+id/root_navigationView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="left|start"
            app:headerLayout="@layout/your_layout_here"
            app:menu="@menu/menu_navigation_view" />