向操作栏添加操作

时间:2016-10-27 09:45:43

标签: android android-layout android-studio

我试图在Android应用中第一次设置操作栏。 我一步一步地按照下面给出的指示: https://developer.android.com/training/appbar/actions.html 但它不起作用...... 我收到此错误消息,如果我在谷歌搜索它,它给了我完全不同的情况比我... (我无法插入整个代码,因为太多,所以我只插入相对于操作栏的那个) enter image description here

这里的清单:

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

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

这里是动作bar.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="@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"/>

</menu>

main_activity.xml:

    <?xml version="1.0" encoding="utf-8"?>
<!--Layout che prende tutto lo schermo-->
<LinearLayout 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:orientation="vertical"
    tools:context="com.example.android.projectcalculator.MainActivity">

    <!--ActionBar in alto-->
    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#ff0000"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</LinearLayout>

MainActivity.java:

package com.example.android.projectcalculator;

import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import static android.R.attr.breadCrumbShortTitle;
import static android.R.attr.data;
import static android.R.attr.onClick;
import static android.webkit.ConsoleMessage.MessageLevel.LOG;

public class MainActivity extends AppCompatActivity {

    //varibile per stampare nelle text view, e per fare i calcoli
    private static String equation;
    private static int neutralElement;
    private static boolean flagNeutralElementBegin;
    private static boolean flagNeutralElementEnd;
    private static boolean flagIsNuberWithDotActive;
    private static int openBracket;

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

2 个答案:

答案 0 :(得分:2)

在MainActivity.java中添加:

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.action_favorite:
            break;
        case R.id.action_settings:
            break;
        default:
            return super.onOptionsItemSelected(item);
    }
    return true;
}

在strings.xml中添加:

<string name="action_settings">Settings</string>
<string name="action_favorite">Favorite</string>

答案 1 :(得分:0)

实现这2个方法

 public boolean onCreateOptionsMenu(Menu menu) {
    this.menu = menu;
    getMenuInflater().inflate(R.menu.menu,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.action_favorite:
            break;
        case R.id.action_settings:
            break;
        default:
            return super.onOptionsItemSelected(item);
    }
    return true;
}