java 如何在扩展 ListActivity 的 Activity 中使用 onCreateOptionsMenu()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27585690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:03:21  来源:igfitidea点击:

How to use onCreateOptionsMenu() in Activity Which Extended ListActivity

javaandroidxmlandroid-studio

提问by Mehmet

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 ActionBarActivityto ListActivitythen, when I run my app on my device, I can not see my create icon (and the top menu which include app name). What's wrong? And idea?

在这一点上,问题开始了。我已将我的超类从ActionBarActivity更改为ListActivity然后,当我在我的设备上运行我的应用程序时,我看不到我的创建图标(以及包含应用程序名称的顶部菜单)。怎么了?和想法?

(Btw I am using Android Studio Ide)

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

采纳答案by CommonsWare

Your theme is probably still based on Theme.AppCompat. If you want to use Theme.AppCompat, you have to inherit from ActionBarActivity. If you want to use ListActivity, you cannot use Theme.AppCompat.

您的主题可能仍然基于Theme.AppCompat. 如果你想使用Theme.AppCompat,你必须继承自ActionBarActivity. 如果你想使用ListActivity,你不能使用Theme.AppCompat

Note that you do not need to use ListActivityto have a ListViewin an activity. Here is a sample projectdemonstrating the use of Theme.AppCompatwith an ActionBarActivityand a ListView. Here is another sample project, the same as the previous one, except that I apply custom tints to the action bar.

请注意,您不需要使用ListActivityListView一个活动。这是一个示例项目,演示了Theme.AppCompatwith anActionBarActivity和 a 的使用ListView这是另一个示例项目,与上一个相同,不同之处在于我将自定义色调应用于操作栏。