简体   繁体   中英

How can I change the menu title in navigation drawer programmatically

Im trying to change the color of the menu title in a navigation drawer programmatically, I've managed to change the color of the items but now I want to change the color of the title. 在此处输入图片说明

I've created all the items dynamically from a data base, in the image I show what it is that I want to change

for(Actividad actividad: actividades) {
                Log.d("PRUEBA", String.valueOf(actividad.get_id()));
                //this is where I create the menu title
                SubMenu menuGroup = menu.addSubMenu(actividad.get_id(), actividad.get_id(), Menu.NONE, actividad.getNombre());
                SpannableString s = new SpannableString(menuGroup.toString());
                s.setSpan(new ForegroundColorSpan(Color.WHITE), 0, s.length(), 0);
                for (Hijo hijo : actividad.getHijos()) {
                    //this is where I create the items
                    menuGroup.add(hijo.get_id(),hijo.get_id(),Menu.NONE,hijo.getNombre());
                }
            }

Any ideas on how can I do it?

Replace this :

 s.setSpan(new ForegroundColorSpan(Color.WHITE), 0, s.length(), 0);

With:

s.setSpan(new TextAppearanceSpan(this, R.style.MyStyle), 0, s.length(), 0);

Define your style in style.xml

<style name="MyStyle">
        <item name="android:textColor">#FFFFFF</item>
 </style>

Hope this helps.

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