How to change the menu in Android programmatically

痴心易碎 提交于 2019-12-10 15:45:49

问题


I have two different menu layouts. I want to change the inflated menu on runtime when a button is clicked.

Initially the menu is set to @menu/navigation. I want it to change to @menu/navigation2 when button is clicked.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="th.ac.sd.sdschoolas.MainActivity">

    <FrameLayout
        android:id="@+id/content_main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <WebView
            android:id="@+id/web_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="152dp"
            android:layout_height="158dp"
            android:layout_x="23dp"
            android:layout_y="46dp"
            app:srcCompat="@mipmap/logo"
            android:padding="20dp"/>
    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />

</LinearLayout>

menu/navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/nav1"
        android:icon="@mipmap/homeicon"
        android:title="@string/title_home" />

    <item
        android:id="@+id/nav2"
        android:icon="@mipmap/newsicon01"
        android:title="@string/title_news" />

    <item
        android:id="@+id/nav3"
        android:icon="@drawable/ic_calendar_page_empty"
        android:title="@string/title_cal" />

    <item
        android:id="@+id/nav4"
        android:icon="@drawable/ic_chat_bubbles"
        android:title="@string/title_sms" />

    <item
        android:id="@+id/nav5"
        android:icon="@mipmap/more"
        android:title="@string/title_more" />

</menu>

menu/navigation2.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/nav1"
        android:icon="@mipmap/seticon"
        android:title="@string/title_setting" />

    <item
        android:id="@+id/nav2"
        android:icon="@mipmap/moreicom"
        android:title="@string/title_more2" />

</menu>

回答1:


The menu can be inflated by overriding the onCreateOptionsMenu method.

You may use an internal state to choose which menu to inflate, e.g.

private int menuToChoose = R.menu.navigation;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(menuToChoose, menu);
    return true;
}

Then, in your OnClickListener you need to change the state and call invalidateOptionsMenu(), e.g.

public void onClick(View v) {
    menuToChoose = R.menu.navigation2;

    invalidateOptionsMenu();
}



回答2:


You can use the Menu's add() api to dynamically add menus

menu.add(0, 0, 0, "Option1").setShortcut('3', 'c');

You can get menu object in API

public boolean onCreateOptionsMenu(Menu menu)



回答3:


Use following code for set menu programmetically...

navigation.inflateMenu(R.menu.name_menu);


来源:https://stackoverflow.com/questions/42804982/how-to-change-the-menu-in-android-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!