如何使用android在navdrawer中设置导航项目图标

时间:2016-10-17 06:48:10

标签: android navigation-drawer

我正在开发一个应用程序,我正在使用android导航抽屉我想用导航列表项显示图标。以下是我试过的代码。请建议我如何在导航菜单中显示导航抽屉项目图标。< / p>

public class Search_ActivityND1 extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    private Toolbar toolbar;
    NavigationView navigationView;

    Context ctx = this;
    String ride_root, cab_type, email, ve_type, region, cdate;
    private boolean loggedIn = false;
    TextView textView, textView1, textView2, tvdate;

    Spinner spinner1, spinner2;
    Button submit, logout;
    private ArrayList<String> students1;
    private ArrayList<String> students2;
    private JSONArray result1, result2, result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nd);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        /*    LayoutInflater inflater = (LayoutInflater) this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View contentView = inflater.inflate(R.layout.search_activity_nd, null, false);
            drawerLayout.addView(contentView, 0);
*/
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        initNavigationDrawer();

        submit = (Button) findViewById(R.id.button_search);
        // logout = (Button) findViewById(R.id.button3_logout);
        textView = (TextView) findViewById(R.id.textView_user);
        tvdate= (TextView)findViewById(R.id.textView_date);

        Calendar cal = Calendar.getInstance(TimeZone.getDefault());
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss a", Locale.getDefault());
        tvdate.setText(cal.getTime().toLocaleString());

        //for current address

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                loggedIn = sharedPreferences.getBoolean(Config.LOGGEDIN_SHARED_PREF, false);
                email = sharedPreferences.getString(Config.EMAIL_SHARED_PREF, "Not Available");
                textView.setText(email);

                //Fetching the boolean value form sharedpreferences
                //If we will get true
                if (loggedIn) {
                    //We will start the Profile Activity
                    submitForm();
                    Intent intent = new Intent(Search_ActivityND1.this, List_Activity1.class);
                    startActivity(intent);
                }
            }
        });


    public void initNavigationDrawer() {
        navigationView = (NavigationView) findViewById(R.id.navigation_view);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {

                int id = menuItem.getItemId();

                switch (id) {
                  case R.id.mybookings:
                        Intent i1 = new Intent(Search_ActivityND1.this, Mybooking.class);
                        startActivity(i1);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.contact:
                        Intent i2 = new Intent(Search_ActivityND1.this, Contact_Activity.class);
                        startActivity(i2);
                        drawerLayout.closeDrawers();
                        break;
                /*    case R.id.privacy:
                        Intent i3 = new Intent(Search_ActivityND1.this, Privacy_Activity.class);
                        startActivity(i3);
                        drawerLayout.closeDrawers();*/
                    case R.id.about:
                        Intent i4 = new Intent(Search_ActivityND1.this, About_Activity.class);
                        startActivity(i4);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.logout:
                        logout();
                        drawerLayout.closeDrawers();
                        break;


                }
                return true;
            }
        });


        SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
        loggedIn = sharedPreferences.getBoolean(Config.LOGGEDIN_SHARED_PREF, false);
        email = sharedPreferences.getString(Config.EMAIL_SHARED_PREF, "Not Available");

        View header = navigationView.getHeaderView(0);
        TextView tv_email = (TextView) header.findViewById(R.id.tv_email);
        tv_email.setText(email);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);

        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

            @Override
            public void onDrawerClosed(View v) {
                super.onDrawerClosed(v);
            }

            @Override
            public void onDrawerOpened(View v) {
                super.onDrawerOpened(v);
            }
        };
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
    }
}

menu_navigation

<?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/mybookings"
            android:title="My Bookings"
            android:icon="@drawable/b"/>
        <item
            android:id="@+id/contact"
            android:title="Contact"
            android:icon="@drawable/c"/>
        <item
            android:id="@+id/privacy"
            android:title="Privacy"
            android:icon="@drawable/pp"/>
        <item
            android:id="@+id/about"
            android:title="About"
            android:icon="@drawable/a"/>

        <item
            android:id="@+id/logout"
            android:title="Logout"
            android:icon="@drawable/l"/>


    </group>
</menu>

navdrawer

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:fitsSystemWindows="true"
        tools:context="com.learn2crack.myapplication.MainActivity"
        xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/blue"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

        <include layout="@layout/search_activity_nd" />

    </android.support.design.widget.CoordinatorLayout>

            <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_gravity="start"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/menu_navigation"/>

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

0 个答案:

没有答案