2016-07-02 4 views
3

Ich arbeite an Android-Anwendung, wo ich einen Button in Aktionsbalken brauche. Dies sind meine Aktivitäten und Java-Dateien. Also, ich brauche eine Schaltfläche auf der rechten Seite meiner Symbolleiste. Vielen Dank im Voraus.So erstellen Sie Schaltfläche in der Aktionsleiste in Android

content_b2_bdeliveries.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.ideabiz.riderapplication.ui.B2BDeliveriesActivity" 
tools:showIn="@layout/activity_b2_bdeliveries"> 
//remaining code; 
</LinearLayout> 

activity_b2_bdeliveries.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 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" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context="com.ideabiz.riderapplication.ui.B2BDeliveriesActivity"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

</android.support.design.widget.AppBarLayout> 

<include layout="@layout/content_b2_bdeliveries" /> 

</android.support.design.widget.CoordinatorLayout> 

B2BDeliveriesActivity.java:

public class B2BDeliveriesActivity extends AppCompatActivity { 
private final String TAG_NAME="B2BActivity"; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_b2_bdeliveries); 
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
    ActionBar actionBar = getSupportActionBar(); 
    //Log.d(TAG_NAME,actionBar.toString()); 
    if (actionBar != null) { 
     try { 
      actionBar.setDisplayHomeAsUpEnabled(true); 
      } catch (Exception e) { 
       Crashlytics.logException(e); 
       Log.d(TAG_NAME, "Null Pointer from setDisplayHomeAsUpEnabled" + e.getMessage()); 
       Utilities.issueTokenUpload("Null Pointer from setDisplayHomeAsUpEnabled" + e, sharedPreferences.getInt("tripId", 999999), sharedPreferences.getString("driverPhoneNo", "9999999999"), sharedPreferences.getString("driverName", "default"), getResources().getString(R.string.version)); 
      } 
    } 
} 

sample requirement picture

+0

i verwendet haben public boolean onOptionsItemSelected (MenuItem-Element) { finish(); Rückkehr wahr; } in meiner B2BDeliveriesActivity.java Seite –

+0

was meinst du mit Knopf? Menütasten? –

+0

HI Ashish, ich brauche einen einzigen Knopf auf meiner rechten Seite der Werkzeugleiste –

Antwort

19
// create an action bar button 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.mymenu, menu); 
    return super.onCreateOptionsMenu(menu); 
} 

// handle button activities 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 

    if (id == R.id.mybutton) { 
    // do something here 
    } 
    return super.onOptionsItemSelected(item); 
} 

und Ihre mymenu.xml sollten in res/Menü Verzeichnis und suchen dies wie:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
<item 
    android:id="@+id/mybutton" 
    android:title="" 
    app:showAsAction="always" 
    android:icon="@drawable/mybuttonicon" 
    /> 
</menu> 
+0

Hallo Onur, wo ich Ihren mymenu.xml-Code in content.xml oder activity.xml verwenden muss? –

+2

Erstellen Sie einfach einen neuen Ordner in Ihrem Res-Ordner namens menu (falls noch nicht vorhanden) und fügen Sie eine neue Datei namens menu.xml hinzu – babadaba

0

Jemand hatte diese Frage beantwortet, bevor

Sie zwei Schritte haben, das zu tun:

  1. erstellen Menü in res/Menü (Name it my_menu.xml)

    <menu xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
        android:id="@+id/action_cart" 
        android:icon="@drawable/cart" 
        android:orderInCategory="100" 
        android:showAsAction="always"/> 
    </menu> 
    
  2. Aufschalten onCreateOptionsMenu und aufblasen Menü

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.my_menu, menu); 
    return true; 
    } 
    

Besuch: How to add button in ActionBar(Android)?

0

eine XML-Datei Machen Sie für Ihre Menü in res/menu Verzeichnis (menu_main.xml in diesem Fall):

<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="com.example.rashish.myapplication.MainActivity"> 
    <item 
     android:id="@+id/action_settings" 
     android:orderInCategory="100" 
     android:title="Button title" 
     android:icon="@drawable/buttonicon" 
     app:showAsAction="never" /> 
</menu> 

dann diese Methoden in Ihrer Aktivitätsklasse hinzu:

@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; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.action_settings) { 
     //process your onClick here 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
2

Wenn Sie UI Ihrer ActionBar anpassen möchten, dann können Sie Toolbar als ActionBar verwenden. Hier sind die Schritte, das gleiche zu tun:

  1. unten Code hinzufügen als Kind von AppBarLayout in Ihrer Tätigkeit Layout xml.

    <android.support.v7.widget.Toolbar 
         android:id="@+id/toolbar" 
         android:layout_width="match_parent" 
         android:layout_height="?attr/actionBarSize" > 
    
         <LinearLayout 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:weightSum="1"> 
    
          <TextView 
           android:id="@+id/title" 
           android:layout_width="0dp" 
           android:layout_height="wrap_content" 
           android:layout_weight="1" 
           android:text="ActionBar Title"/> 
    
          <Button 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" /> 
    
         </LinearLayout> 
    
        </android.support.v7.widget.Toolbar> 
    
  2. Machen Sie Ihre Toolbar als ActionBar durch den Code unten in Ihrer Aktivität Hinzufügen onCreate (Methode).

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    
  3. In AndroidManifest.xml, fügen Sie Ihre Aktivität nach Thema:

    android:theme="@style/AppTheme.NoActionBar" 
    
  4. In styles.xml add folgenden Code:

    <style name="AppTheme.NoActionBar"> 
        <item name="windowActionBar">false</item> 
        <item name="windowNoTitle">true</item> 
    </style>