2016-09-24 24 views
0

我正在尝试开发一个简单的天气预报应用程序。 Android Studio v2.2 最低SDK设置为API10:姜饼。搜索城市选项没有出现在Android Studio

问题 - 我需要对我的第一page.So一个搜索城市选项我只是编辑的menu_main.xml文件,并添加该选项。 我的问题是,搜索城市选项出现在menu_main.xml的'设计'和'预览'部分,但是当我检查我的activity_main.xml的'design'或'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作为它只是包含了一些简单TextViews 。

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

任何帮助是appreciated..thanks ..

回答

1

我觉得现在的问题是不是在你的活动中为菜单充气。添加到您的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); 
} 

为了创建动作条按钮,检查:Creating a button in Android Toolbar