android - 菜单项未显示在操作栏中

时间:2016-12-10 11:59:41

标签: java android menu android-actionbar

我试图添加菜单项"已完成"在动作栏中。我刚从另一个显示菜单项的活动中复制并修改了代码。所以它应该正常工作。

这是活动布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/md_blue_600"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways"/>
    </android.support.design.widget.AppBarLayout>


    <LinearLayout
        android:id="@+id/activity_credits"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:mContext="com.getsaveit.saveit.activities.CreditsActivity">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="18sp"
            android:layout_marginBottom="@dimen/activity_vertical_margin"
            android:text="@string/affiliator_category"/>

        <com.toptoche.searchablespinnerlibrary.SearchableSpinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:hintText="Select category"
            android:id="@+id/affiliator_category_spinner"/>

        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/rectangular_shap"
            android:id="@+id/affiliators_listview"/>
    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

choose_affiliator_menu.xml档案:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:title="@string/done"
        android:orderInCategory="1"
        android:id="@+id/next_btn"
        app:showAsAction="always" />
</menu>

java代码本身:

public class CreditsActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credits);

        Toolbar actionBar = (Toolbar) findViewById(R.id.toolbar);
        actionBar.setTitle("Memberships");

        //....
    }

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

1 个答案:

答案 0 :(得分:3)

初始化工具栏后应使用以下方法。

  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  setSupportActionBar(toolbar);