菜单未显示在Android Studio工具栏上

时间:2019-05-05 21:51:59

标签: android view menu toolbar

我在Android Studio中创建了一个选项卡式活动和导航抽屉。我希望菜单选项出现在我的第二个选项卡上,但是它没有出现在工具栏上,因为我使用的是片段类,而我的工作代码仅适用于AppCompatActivity类。

这是我第二个标签页的代码:

 private TaskDbHelper mHelper;
    private ListView mTaskListView;
    private ArrayAdapter<String> mAdapter;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view4 = inflater.inflate(R.layout.events_layout, container, false);
        return view4;
    }


    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        mHelper = new TaskDbHelper(getActivity());
        mTaskListView = (ListView) getView().findViewById(R.id.list_todo);
        updateUI();
    }

    private void updateUI() {
        ArrayList<String> taskList = new ArrayList<>();
        SQLiteDatabase db = mHelper.getReadableDatabase();
        Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
                new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE},
                null, null, null, null, null);
        while (cursor.moveToNext()) {
            int idx = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
            taskList.add(cursor.getString(idx));
        }

        if (mAdapter == null) {
            mAdapter = new ArrayAdapter<String>(getActivity(),
                    R.layout.item_todo,
                    R.id.task_title,
                    taskList);
            mTaskListView.setAdapter(mAdapter);
        } else {
            mAdapter.clear();
            mAdapter.addAll(taskList);
            mAdapter.notifyDataSetChanged();
        }

        cursor.close();
        db.close();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.main_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add_task:
                final EditText taskEditText = new EditText(getActivity());
                AlertDialog dialog = new AlertDialog.Builder(getActivity())
                        .setTitle("Add a new task")
                        .setMessage("What do you want to do next?")
                        .setView(taskEditText)
                        .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String task = String.valueOf(taskEditText.getText());
                                SQLiteDatabase db = mHelper.getWritableDatabase();
                                ContentValues values = new ContentValues();
                                values.put(TaskContract.TaskEntry.COL_TASK_TITLE, task);
                                db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
                                        null,
                                        values,
                                        SQLiteDatabase.CONFLICT_REPLACE);
                                db.close();
                            }
                        })
                        .setNegativeButton("Cancel", null)
                        .create();
                dialog.show();
                updateUI();
                return true;

            default:
                return super.onOptionsItemSelected(item);

        }
    }

    public void deleteTask(View view) {
        View parent = (View) view.getParent();
        TextView taskTextView = (TextView) parent.findViewById(R.id.task_title);
        String task = String.valueOf(taskTextView.getText());
        SQLiteDatabase db = mHelper.getWritableDatabase();
        db.delete(TaskContract.TaskEntry.TABLE,
                TaskContract.TaskEntry.COL_TASK_TITLE + " = ?",
                new String[]{task});
        db.close();
        updateUI();
    }


}

这是我的菜单选项代码:

<?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_add_task"
        android:icon="@android:drawable/ic_menu_add"
        android:title="Add Task"
        android:visible="true"
        app:showAsAction="ifRoom" >
        <menu>
            <item
                android:icon="@drawable/ic_menu_add"
                android:title="@string/action_settings"
                app:showAsAction="always" />
            <item
                android:icon="@drawable/ic_menu"
                android:title="@string/action_settings"
                app:showAsAction="always" />
        </menu>
    </item>
</menu>

最后,这是我的标签内容:

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <ListView
        android:id="@+id/list_todo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />



</RelativeLayout>

如果您还没有弄清楚,我使用了https://www.sitepoint.com/starting-android-development-creating-todo-app/之类的基本TODO列表教程,但它们并未实现片段类。 (或者也许这不是问题,我刚刚弄乱了工具栏?)

This is my output

This is my desired output

1 个答案:

答案 0 :(得分:0)

菜单的

oncreateOptionsMenu和onOptionsItemSelected应该位于“活动”中。如果要在片段中包含菜单,则必须调用setHasOptionsMenu(true)来显示菜单