2016-04-10 18 views
0

Ok Jungs, bevor ich beginne ich will Ihnen nur sagen, ich habe Android Studio 2.1 5 Vorschau. Das Problem, das ich habe, ist mit dem Hinzufügen von Aktionstasten. Und ich folgte diesen Tutorial von der offiziellen Entwickler-Dokumentation:Android Hinzufügen von Aktionsschaltflächen

http://developer.android.com/intl/es/training/appbar/actions.html#add-actions

So, nachdem ich mit dem Tutorial ging ich zu diesem Code angekommen:

MainActivity.java:

package com.example.amanuel.actionbar; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.support.v7.widget.ToolbarWidgetWrapper; 
import android.view.MenuItem; 

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.my_toolbar); 
     toolbar.setLogo(R.mipmap.ic_launcher); 
     toolbar.setLogoDescription("Logo"); 
     setSupportActionBar(toolbar); 
    } 

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

    } 
    } 
} 

res/menü/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"> 
    <!-- "Mark Favorite", should appear as action button if possible --> 
    <item 
    android:id="@+id/action_favorite" 
    android:icon="@mipmap/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"/> 
</menu> 

AndroidManifest.xml:

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

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

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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.example.amanuel.actionbar.MainActivity"> 

    <android.support.v7.widget.Toolbar 
    android:id="@+id/my_toolbar" 
    android:layout_width="fill_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" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentEnd="true" /> 

</RelativeLayout> 

Also, wenn ich das alles setzen und es auf genymotion laufen bekomme ich diese:

enter image description here

Na ok, ich meine Aktionsleiste aber wo in der Welt sind meine Aktionsknöpfe? Wie es mir auf die Entwickler-Dokumentation zeigte:

enter image description here

Auch ich verstehe nicht, warum die ActionBar in der Mitte ist .. Seine normalerweise nehme an der Spitze zu sein, wie eine normale Symbolleiste. Das verwirrt mich nur, bitte hilf mir. Vielen Dank!

Antwort

1

Sie haben nichts, was auf Ihre menu.xml Ressource verweist.

Überschreiben Sie onCreateOptionsMenu(), und verwenden Sie getMenuInflater().inflate(menu, R.menu.menu).

+0

Wow, vielen Dank! Nur eine kurze Frage ... Weißt du, warum die Aktionsleiste ein bisschen oben auf der Seite ist? Kann ich es so machen, als wäre ich ganz oben, genau wie eine normale Toolbar? – amanuel2

+0

@AmanuelBogale: Von meinem Kopf her weiß ich nicht, warum deine schwimmt, obwohl ich 'Toolbar' nicht viel benutzt habe, ganz zu schweigen von dem' appcompat-v7' Backport. – CommonsWare

+0

oh well ..... ich denke ich muss danach suchen .. – amanuel2