menuInflater在onCreateOptionsMenu中创建菜单不起作用

时间:2016-05-10 07:53:13

标签: android android-menu

我使用了menuInflater在您活动的onCreateOptionsMenu方法中创建菜单。我用它来显示工具栏上的箭头以显示日历,以便用户可以转到上个月或下个月。但由于某种原因,箭头没有显示出来 请别人帮忙。

MonthGridActivity:

private MonthGridFragment monthGridFragment;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_calendar_grid, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_previous:
            monthGridFragment.loadLastMonth();
            return true;
        case R.id.action_next:
            monthGridFragment.loadNextMonth();
            return true;
        case R.id.all_events:
            monthGridFragment.showAllEvents();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

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

    monthGridFragment = new MonthGridFragment();
    getSupportFragmentManager().beginTransaction()
            .add(R.id.activity_calendar_grid_container, monthGridFragment)
            .commit();
}

menu_calendar_grid.xml

<item
    android:id="@+id/action_previous"
    android:title="@string/prev_month"
    android:orderInCategory="100"
    app:showAsAction="ifRoom"
    android:icon="@drawable/arrow_previous" />
<item
    android:id="@+id/action_next"
    android:title="@string/next_month"
    android:orderInCategory="100"
    app:showAsAction="ifRoom"
    android:icon="@drawable/arrow_next" />
<item
    android:id="@+id/all_events"
    android:title="@string/view_all_events"
    android:orderInCategory="101"
    app:showAsAction="never"
    android:icon="@android:drawable/ic_menu_view" />

MonthGridFragment.java

 public void loadNextMonth() {
    calendar.setTime(CalUtil.addMonth(calendar.getTime(), 1));
    refresh();
}

public void loadLastMonth() {
    calendar.setTime(CalUtil.subtractMonth(calendar.getTime(), 1));
    refresh();
}

activity_calendar_grid.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
  tools:ignore="MergeRootFrame">

<android.support.v7.widget.Toolbar
    android:id="@+id/myActivity_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

fragment_calendar_grid.xml

<FrameLayout 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:orientation="vertical"
android:id="@+id/calendar_grid_layout">

<include layout="@layout/calendar_grid_header" />


<GridView
    android:paddingTop="1dp"
    android:gravity="center"
    android:layout_gravity="center"
    android:layout_margin="0dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/calendar_grid_view"
    android:numColumns="7"
    android:stretchMode="columnWidth"
    android:background="@color/white_gray"
    android:horizontalSpacing="1dp"
    android:verticalSpacing="1dp" />

1 个答案:

答案 0 :(得分:1)

onCreate方法中创建一个工具栏:

Toolbar toolbar = (Toolbar) findViewById(R.id.myActivity_toolbar);
setActionBar(toolbar);

所以在布局中添加视图:

<Toolbar
    android:id="@+id/myActivity_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<强> UPD:

您必须使用支持库中的Toolbar或标准库。如果MonthGridActivityActivity延伸,请在布局中使用Toolbar(而不是android.support.v7.widget.Toolbar)。否则MonthGridActivity必须从AppCompatActivity

延长