Android Studio 中未显示“搜索城市”选项

Search City option not showing up in Android Studio

我正在尝试开发一个简单的天气预报应用程序。 Android Studio v2.2 最低 SDK 设置为 API10:Gingerbread。

问题 - 我需要一个 搜索城市 选项 page.So 我只是编辑了 menu_main.xml 文件并添加了该选项。 我的问题是搜索城市选项出现在 menu_main.xml 的 'design' 和 'preview' 部分,但当我检查 'design'或我的 activity_main.xml 的 'preview' 部分。

我也在清单中将主题设置为@style/AppTheme。

In that red highlighted part I want my menu to appear..

我的菜单文件是-

    <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=".MainActivity" >

    <item

    android:id="@+id/change_city"
    android:orderInCategory="100"
    android:title="@string/change_city"

    app:showAsAction="always"/>

    </menu>

我的mainactivity.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#ff1ba1ee"``
    tools:context="com.example.hp.theweatherapp.MainActivity">

我没有添加整个 xml 因为它只包含简单的 TextView。

我的MainActivity.java是-

    package com.example.hp.theweatherapp;

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    }

Manifests.xml 是-

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

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <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 中展开菜单。添加到您的 MainActivity.java 此代码:

@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. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.change_city) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

对于创建 ActionBar 按钮,勾选: