studio personalizar icono desplegable boton bar agregar android menuitem android-toolbar appcompatactivity

personalizar - navigation drawer android studio



Cómo mostrar un elemento de menú con icono y texto en AppCompatActivity (7)

Agregando a la share de dev_ry, hay una forma mucho más fluida sin usar la reflexión simplemente lanzando y suprimiendo la advertencia de API restringida:

import android.support.v7.view.menu.MenuBuilder; @SuppressLint("RestrictedApi") @Override public boolean onCreateOptionsMenu(Menu menu) { if (menu instanceof MenuBuilder) { ((MenuBuilder) menu).setOptionalIconsVisible(true); } getMenuInflater().inflate(R.menu.menu, menu); return super.onCreateOptionsMenu(menu); }

Intenté diferentes combinaciones en el archivo xml:

<menu 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" tools:context=".MainActivity"> <item android:id="@+id/action_create_alarm" android:icon="@drawable/ic_action_accept" android:orderInCategory="100" android:title="@string/menu_create_alarm" app:showAsAction="ifRoom|withText" /> </menu>

o

<menu 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" tools:context=".MainActivity"> <item android:id="@+id/action_create_alarm" android:icon="@drawable/ic_action_accept" android:orderInCategory="100" android:title="@string/menu_create_alarm" app:showAsAction="always|withText" /> </menu>

o

<menu 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" tools:context=".MainActivity"> <item android:id="@+id/action_create_alarm" android:icon="@drawable/ic_action_accept" android:orderInCategory="100" android:title="@string/menu_create_alarm" app:showAsAction="withText" /> </menu>

o

<menu 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" tools:context=".MainActivity"> <item android:id="@+id/action_create_alarm" android:icon="@drawable/ic_action_accept" android:orderInCategory="100" android:title="@string/menu_create_alarm" android:showAsAction="always|withText" /> </menu>

Traté de configurarlo programáticamente

@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){ MenuItem item = menu.add(R.string.menu_create_alarm); item.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT|MenuItem.SHOW_AS_ACTION_IF_ROOM); item.setIcon(R.drawable.ic_action_accept); item.setOnMenuItemClickListener( new OnMenuItemClickListener(){ @Override public boolean onMenuItemClick(MenuItem item){ saveAlarm(); return true; } } ); // inflater.inflate(R.menu.menu_create_alarm, menu); super.onCreateOptionsMenu(menu, inflater); }

o

<menu 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" tools:context=".MainActivity"> <item android:id="@+id/action_create_alarm" android:icon="@drawable/ic_action_accept" android:orderInCategory="100" android:title="@string/menu_create_alarm" android:showAsAction="always|withText" app:showAsAction="always|withText" /> </menu>

Sin embargo, solo aparece el icono. Y hay mucho espacio, porque no configuré el título de la barra de herramientas. Eliminar menús y volver a colocarlos con el botón dentro de la barra de herramientas no es adecuado.
¿Cómo mostrar texto?


Necesita agregar tools:context="your class" a la etiqueta del menú

menu.xml

<?xml version="1.0" encoding="utf-8"?> <menu 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" tools:context=".activities.BaseActivity"> <item android:id="@+id/action_notification1" android:icon="@drawable/three" android:title="action_notification" app:showAsAction="always"> <menu> <item android:id="@+id/profile" android:icon="@drawable/profile" android:orderInCategory="100" android:title="PROFILE" /> <item android:id="@+id/c" android:icon="@drawable/correct_tick" android:orderInCategory="100" android:title="COMPLETED TRIPS" /> <item android:id="@+id/app" android:icon="@drawable/report_issue" android:orderInCategory="100" android:title="REPORT ISSUES" /> <item android:id="@+id/r" android:icon="@drawable/correct_tick" android:orderInCategory="100" android:title="REACHED CENTER" /> <item android:id="@+id/pdf" android:icon="@drawable/pdf_image" android:orderInCategory="100" android:title="BAG INFO" /> <item android:id="@+id/l" android:icon="@drawable/logout" android:orderInCategory="100" android:title="LOGOUT" /> </menu> </item> </menu>

Override onCreateOptionsMenu() Método

@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); menu.getItem(0).getSubMenu().getItem(3).setVisible(false); menu.getItem(0).getSubMenu().getItem(4).setVisible(true); return super.onCreateOptionsMenu(menu); }

Debería escribir tool:context to menu tag y luego ejecutar, obtendrá iconos antes de su texto.


prueba estos dos juntos

app:showAsAction="always|withText" android:showAsAction="always|withText"


xml:

<menu 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" tools:context=".MainActivity"> <item android:id="@+id/action_create_alarm" android:icon="@drawable/ic_action_accept" android:orderInCategory="100" android:title="@string/menu_create_alarm" android:showAsAction="always|withText" app:showAsAction="always|withText" /> </menu>

Java:

@Override public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.menu_frg_safetybox, menu); super.onCreateOptionsMenu(menu, inflater); }


''always | withText'' funcionará si hay suficiente espacio, de lo contrario solo colocará el ícono. Puede verlo girando el teléfono al modo horizontal ...

<item android:id="@+id/action_create_alarm" android:icon="@drawable/ic_launcher" android:title="Save" app:showAsAction="always|withText" />


protected boolean onPrepareOptionsPanel(View view, Menu menu) { if (menu != null) { if (menu.getClass().getSimpleName().equals("MenuBuilder")) { try { Method m = menu.getClass().getDeclaredMethod( "setOptionalIconsVisible", Boolean.TYPE); m.setAccessible(true); m.invoke(menu, true); } catch (Exception e) { Log.e(getClass().getSimpleName(), "onMenuOpened...unable to set icons for overflow menu", e); } } } return super.onPrepareOptionsPanel(view, menu); }


@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. // getMenuInflater().inflate(R.menu.menu_patient_home_screen, menu); menu.add(0, 1, 1, menuIconWithText(getResources().getDrawable(R.mipmap.user_2), getResources().getString(R.string.action_profile))); menu.add(0, 2, 2, menuIconWithText(getResources().getDrawable(R.mipmap.add_user), getResources().getString(R.string.action_add_user))); menu.add(0, 3, 3, menuIconWithText(getResources().getDrawable(R.mipmap.switch_profile), getResources().getString(R.string.action_switch_profile))); menu.add(0, 4, 4, menuIconWithText(getResources().getDrawable(R.mipmap.logout), getResources().getString(R.string.action_sign_out))); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement switch (item.getItemId()) { case 1: Toast.makeText(PatientHomeScreen.this, "Profile is Clicked", Toast.LENGTH_SHORT).show(); return true; case 2: Toast.makeText(PatientHomeScreen.this, "Add New User is Clicked", Toast.LENGTH_SHORT).show(); return true; case 3: Toast.makeText(PatientHomeScreen.this, "Switch Profile is Clicked", Toast.LENGTH_SHORT).show(); return true; case 4: Toast.makeText(PatientHomeScreen.this, "Sign Out is Clicked", Toast.LENGTH_SHORT).show(); return true; } return super.onOptionsItemSelected(item); } private CharSequence menuIconWithText(Drawable r, String title) { r.setBounds(0, 0, r.getIntrinsicWidth(), r.getIntrinsicHeight()); SpannableString sb = new SpannableString(" " + title); ImageSpan imageSpan = new ImageSpan(r, ImageSpan.ALIGN_BOTTOM); sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return sb; }

Espero que esto te ayudará