简体   繁体   English

如何在Activity中使用onCreateOptionsMenu()哪个扩展ListActivity

[英]How to use onCreateOptionsMenu() in Activity Which Extended ListActivity

I have a problem about my android app project. 我有一个关于我的Android应用程序项目的问题。

I have a MainActivity which is below: 我有一个MainActivity,如下所示:

public class MainActivity extends ListActivity {

    private NotesDataSource datasource;
    List<NoteItem> notesList;



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

        datasource = new NotesDataSource(this);

        refreshDisplay();
    }

    private void refreshDisplay() {
        notesList = datasource.findAll();
        ArrayAdapter<NoteItem> adapter = new ArrayAdapter<NoteItem>(this, R.layout.list_item_layout, notesList);
        setListAdapter(adapter);
    }


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


}

And Also I have a menu_main.xml: 而且我还有一个menu_main.xml:

<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/action_create"
        android:title="@string/action_create"
        android:orderInCategory="100"
        app:showAsAction="always|withText"
        android:icon="@drawable/create_note"/>
</menu>

At this point the problem is starting. 此时问题就出现了。 I have changed my superclass from ActionBarActivity to ListActivity then, when I run my app on my device, I can not see my create icon (and the top menu which include app name). 我已经将我的超类从ActionBarActivity更改为ListActivity,然后,当我在我的设备上运行我的应用程序时,我看不到我的创建图标(以及包含应用程序名称的顶级菜单)。 What's wrong? 怎么了? And idea? 想法?

(Btw I am using Android Studio Ide) (顺便说一句,我使用的是Android Studio Ide)

Your theme is probably still based on Theme.AppCompat . 您的主题可能仍然基于Theme.AppCompat If you want to use Theme.AppCompat , you have to inherit from ActionBarActivity . 如果要使用Theme.AppCompat ,则必须从ActionBarActivity继承。 If you want to use ListActivity , you cannot use Theme.AppCompat . 如果要使用ListActivity ,则不能使用Theme.AppCompat

Note that you do not need to use ListActivity to have a ListView in an activity. 请注意,您不需要使用ListActivity在活动中包含ListView Here is a sample project demonstrating the use of Theme.AppCompat with an ActionBarActivity and a ListView . 这是一个示例项目,演示了Theme.AppCompatActionBarActivityListView Here is another sample project , the same as the previous one, except that I apply custom tints to the action bar. 这是另一个示例项目 ,与前一个相同,只是我将自定义色调应用于操作栏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM