简体   繁体   中英

Change Icon of MenuItem Navigation Drawer Android Programatically?

Hello I create Navigation Drawer in my App. Now I have quetion how to change each Icon of MenuItem Navigation Drawer Programatically? if in Xml the menu like this:

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

    <group android:checkableBehavior="none">
        <item
            android:id="@+id/nav_input_berita"
            android:icon="@drawable/ic_nav_input_berita"
            android:title="@string/input_berita" />
        <item
            android:id="@+id/nav_logout"
            android:icon="@drawable/ic_nav_logout"
            android:title="@string/logout" />
    </group>
</menu>

and the Activity like this :

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

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

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setIc
        navigationView.setNavigationItemSelectedListener(this);
        setFragmentArtikel();
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_input_berita) {

        } else if (id == R.id.nav_logout) {

        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

}

so how the programaticaly. Change Icon android:icon="@drawable/ic_nav_input_berita" and android:icon="@drawable/ic_nav_input_logout" ? sorry for my English

Just do this:

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Menu menu = navigationView.getMenu();
MenuItem nav_input_beritaItem = menu.findItem(R.id.nav_input_berita);
nav_input_beritaItem .setIcon(R.drawable.ic_nav_input_berita);
MenuItem nav_logoutItem = menu.findItem(R.id.nav_input_logout);
nav_logoutItem .setIcon(R.drawable.ic_nav_input_logout);

That's all

只需使用toolbar.setNavigationIcon(R.drawable.ic_align_left);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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