C'è un modo per aggiungere un pulsante in alto a destra del mio ActionBar
, come sono le impostazioni predefinite Button
? Ho rimosso le impostazioni Button
ma vorrei aggiungere una personalizzata Button
al suo posto.Aggiungi un pulsante in alto a destra della barra delle azioni
16
A
risposta
48
È possibile aggiungere un pulsante per la modifica/creazione del file XML Menu:
<?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">
<item
android:id="@+id/action_name"
android:icon="@drawable/you_resource_here"
android:title="Text to be seen by user"
app:showAsAction="always"
android:orderInCategory="0"/>
</menu>
Poi nella vostra attività, se è stato creato un nuovo file il vostro bisogno di modificare onCreateOptionsMenu
@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_main, menu);
return true;
}
e si può modificare ciò che le azioni fanno nel seguente metodo:
@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
if (id == R.id.action_name) {
return true;
}
return super.onOptionsItemSelected(item);
}
0
Questo potrebbe essere più semplice, tuttavia io uso un barra degli strumenti invece:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_name:
//your code
break;
}
return super.onOptionsItemSelected(item);
}
Grazie! Ci proverò un po 'e tornerò – Brejuro