Android Toolbar set Navigationicon icon does not show, fails, does not respond, absolutely works

Source: Internet
Author: User

Recently developed Android app, the program used the toolbar control, want to set the toolbar Navigationicon icon but how to set the icon does not work, always show the control itself.
Abnormal:
Setting android:navigationicon= "@drawable/icon" in Layout XML or setting in code cannot be customized to Navigationicon pictures that you want to set.
The code settings are navigationicon as follows:


Toolbar Toolbar = (Toolbar) Findviewbyid (R.id.my_toolbar);
Toolbar.setnavigationicon (R.drawable.icon);
Toolbar.settitle ("Title");
Toolbar.setsubtitle ("Sub");
Toolbar.setlogo (R.drawable.ic_launcher);
Setsupportactionbar (toolbar);

Solution:

If you set the toolbar setting Navigationicon in an XML layout, you now need to discard this practice, otherwise you will never be able to display the navigationicon you set;
You must use the Code method:


Setcontentview (r.layout.activity_main);
Toolbar Toolbar = (Toolbar) Findviewbyid (R.id.toolbar);
Toolbar.settitle ("");
Toolbar.setsubtitle ("");
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 ();
Toolbar.setnavigationicon (R.mipmap.userphoto);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.