2016-12-23 3 views
1

Ich habe die Android-Entwickler Seite auf "Einrichten der App-Leiste" verknüpft here. Das Problem ist, ich kann es nicht richtig machen und der Bildschirm zeigt eine seltsame Werkzeugleiste ohne Überlaufmenü. Ich habe einige andere Antworten gelesen und um zu klären, verwende ich Moto G2 zum Testen. Es enthält keine Optionen auf der Hardware-Seite.Toolbar Zeigt das Überlaufmenü nicht

Mein Code ist wie folgt: 1. MainActivity.java

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
}} 
  1. activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    android:id="@+id/activity_main" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:paddingBottom="@dimen/activity_vertical_margin" 
 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
 
    android:paddingRight="@dimen/activity_horizontal_margin" 
 
    android:paddingTop="@dimen/activity_vertical_margin" 
 
    tools:context="com.example.android.toolbardemo.MainActivity"> 
 

 
    <android.support.v7.widget.Toolbar 
 
     android:id="@+id/toolbar" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="?attr/actionBarSize" 
 
     android:background="?attr/colorPrimary" 
 
     android:elevation="4dp" 
 
     android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 
 

 
    <TextView 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:text="Hello World!" /> 
 
</RelativeLayout>

  1. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 
    package="com.example.android.toolbardemo"> 
 

 
    <application 
 
     android:allowBackup="true" 
 
     android:icon="@mipmap/ic_launcher" 
 
     android:label="@string/app_name" 
 
     android:supportsRtl="true" 
 
     android:theme="@style/Theme.AppCompat.Light.NoActionBar"> 
 
     <activity android:name=".MainActivity"> 
 
      <intent-filter> 
 
       <action android:name="android.intent.action.MAIN" /> 
 

 
       <category android:name="android.intent.category.LAUNCHER" /> 
 
      </intent-filter> 
 
     </activity> 
 
    </application> 
 

 
</manifest>

Die Seite Android Developers erwähnen nichts über ein Menü xml und Sachen zu schaffen, damit ich sollte atleast die Grundeinstellungen Abbildung erscheinen. Hier

ist ein Bild von dem, was ich sehe: No Overflow menu

Antwort

1

Ihre RelativeLayout Um dies zu ändern:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.android.toolbardemo.MainActivity"> 

und Sie müssen diesen Code auf Ihre Aktivität hinzufügen

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.search_menu, menu); 
    return true; 
} 
+0

Excellent setzen! Vielen Dank @Amir_P! Ich habe ungefähr 2,5 Stunden damit verbracht, dieses Überlaufmenü zum Laufen zu bringen! –

+0

Danke! Der offizielle Entwicklerleitfaden ist veraltet. Es sollte aktualisiert werden, um Ihr 'onCreateOptionsMenu (...)' hinzuzufügen. –

0

Sie benötigen Setzen Sie Ihre toolbar als ActionBar wie diese setSupportActionBar(toolbar) und überschreiben Sie onCreateOptionsMenu in Aktivität wie folgt aus:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_search, menu); 
    return true; 
} 

In Ihrem menu.xml Datei benötigen Sie app:showAsAction="never"

<item android:id="@+id/action_search" 
    android:title="@string/search" 
    app:actionViewClass="android.support.v7.widget.SearchView" 
    app:showAsAction="never"/> 
Verwandte Themen