2015-07-13 11 views

Odpowiedz

48

Możesz dodać przycisk przez edycji/tworzenia pliku 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"> 

    <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> 

Następnie w swojej działalności, jeśli tworzony jest nowy plik swoje potrzeby edycji 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; 
} 

i ty można edytować czynności wykonywane w następujący sposób:

@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

Dzięki! Spróbuję tego trochę i wrócę – Brejuro

0

To może być łatwiejsze, ale używam zamiast paska narzędzi:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch(item.getItemId()) { 
     case R.id.action_name: 
      //your code 
      break; 
    } 
    return super.onOptionsItemSelected(item); 
} 
Powiązane problemy