如何将操作栏项添加到第二个活动

时间:2014-12-02 14:00:04

标签: android android-actionbar

这是我的项目(How can i start a different activity on item click from a custom listview?) 我能够解决它。 但是,当我尝试将搜索等操作栏项添加到第二个活动时,项目没有显示。我知道这是一个简单的程序,但我卡住了。我遵循了开发人员的指南(https://developer.android.com/training/basics/actionbar/adding-buttons.html)但没有成功。 任何帮助将不胜感激。

这里是 second activity code

public class School extends ActionbarActivity{
  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.school);


        Intent newActivity1=new Intent();
        setResult(RESULT_OK, newActivity1);
        //finish();
    }

}

main_activity_actions.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          android:showAsAction="ifRoom" />
</menu>

1 个答案:

答案 0 :(得分:2)

尝试将您的活动扩展到ActionBarActivity:

public class School extends ActionBarActivity

编辑:我已经测试了这段代码并且它正在为我工​​作:

        <?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">
       <item android:id="@+id/action_search"
              android:icon="@drawable/ic_action_search"
              android:title="@string/action_search"
              app:showAsAction="ifRoom" />
    </menu>