如何以编程方式更改Android中的菜单 [英] How to change the menu in Android programmatically

查看:73
本文介绍了如何以编程方式更改Android中的菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种不同的菜单布局.我想在单击按钮时在运行时更改膨胀菜单.

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

最初,菜单设置为@menu/navigation.我希望单击按钮时将其更改为@menu/navigation2.

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>

菜单/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>

菜单/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>

推荐答案

可以通过覆盖onCreateOptionsMenu方法来放大菜单.

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;
}

然后,在您的OnClickListener中,您需要更改状态并致电invalidateOptionsMenu(),例如

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();
}

这篇关于如何以编程方式更改Android中的菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆