37

Ich habe android.support.v7.widget.Toolbar in meiner App mit unter Code, jetzt möchte ich eine Schaltfläche am rechten Ende der Symbolleiste, aber nicht in der Lage, dies zu tun.Android v7 Symbolleiste Tastenausrichtung

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/accent_color" 
    android:minHeight="?attr/actionBarSize" 
    android:layout_alignParentTop="true" 
    tools:context=".MyActivity" 
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/showevents" 
     android:textSize="12sp" 
     android:background="@null" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:textColor="@color/white" 
     android:text="UPCOMING \nEVENTS"/> 
</android.support.v7.widget.Toolbar> 

Ich habe die unten auch hinzugefügt, aber es ist nicht nach rechts bewegt zu werden.

android:layout_alignParentEnd="true" 
android:layout_alignParentRight="true" 

angebaute Bild als Referenz:

enter image description here

Antwort

94

Sie sollten android:layout_gravity="right" für den Button hinzufügen:

 <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:id="@+id/showevents" 
     android:textSize="12sp" 
     android:background="@null" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:textColor="#FFF" 
     android:text="UPCOMING \nEVENTS"/> 

enter image description here

+0

Besser den Stil der Taste 'style = "@ style/Widget.AppCompat.Button.Borderless"' oder 'Hintergrund =" android: attr/selectableItemBackground "' anstelle eines @null-Hintergrunds, um bei Berührung einen Welleneffekt zu erhalten, ansonsten scheint die Schaltfläche deaktiviert zu sein. – Roel

+1

Tipp: 'android: textAllCaps =" true "' könnte verwendet werden, anstatt alle caps 'android: text' selbst zu schreiben. Materialbezogene Schaltflächen enthalten diese automatisch, aber bei älteren Geräten ist es normal, dass sie keine Großbuchstaben haben. – TWiStErRob

+1

@Roel, '? SelectableItemBackgroundBorderless', um beide zu haben. – WindRider

5

Oder Bild oben rechts:

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:minHeight="?attr/actionBarSize" 
    android:background="@color/colorPrimary" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:title="Edit Note"> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:id="@+id/submitEditNote" 
     android:src="@android:drawable/ic_menu_send" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" /> 
</android.support.v7.widget.Toolbar> 

Ich hoffe, es

0

hilft Ihr Weg richtig ist, aber einen Blick auf offiziellen doc nehmen: (Resourcs)

Menü Erstellen der Objekte:

<!-- "Mark Favorite", should appear as action button if possible --> 
<item 
    android:id="@+id/action_favorite" 
    android:icon="@drawable/ic_favorite_black_48dp" 
    android:title="@string/action_favorite" 
    app:showAsAction="ifRoom"/> 

<!-- Settings, should always be in the overflow --> 
<item android:id="@+id/action_settings" 
     android:title="@string/action_settings" 
     app:showAsAction="never"/> 

hinzufügen Menü Aktion von ID

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.action_settings: 
      // User chose the "Settings" item, show the app settings UI... 
      return true; 

     case R.id.action_favorite: 
      // User chose the "Favorite" action, mark the current item 
      // as a favorite... 
      return true; 

     default: 
      // If we got here, the user's action was not recognized. 
      // Invoke the superclass to handle it. 
      return super.onOptionsItemSelected(item); 

    } 
}