如何在Java File中设置菜单项标题?

当前关闭:菜单工作正常。

我想要:我想在MainActivity.java中设置菜单标题

访问:1. activitymain.xml 2. MAinActivity.java 3. menu_nevigation.xml

我可以在MainActivity.java中设置菜单项标题吗,因为我想在某些情况下更改标题,如何在Java文件中设置标题...

activity_Main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Home"
        android:textSize="50sp"
        android:textStyle="bold"
        android:layout_centerInParent="true" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bottom_navigation"
        app:itemBackground="@color/colorcustom"
        app:itemTextColor="@drawable/selector"
        app:itemIconTint="@drawable/selector"
        app:menu="@menu/menu_navigation"
        app:labelVisibilityMode="labeled"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

        //Initialize And Assign Variable
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);

        //Set Home Selected
        bottomNavigationView.setSelectedItemId(R.id.home);

        //Perform ItemSelected
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.about:
                    startActivity(new Intent(getApplicationContext()
                            ,about.class));
                    overridePendingTransition(0,0);
                    return true;
                    case R.id.home:
                        return true;
                    case R.id.term:
                        startActivity(new Intent(getApplicationContext()
                                ,term.class));
                        overridePendingTransition(0,0);
                        return true;
                        case R.id.test:
                        startActivity(new Intent(getApplicationContext()
                                ,test.class));
                        overridePendingTransition(0,0);
                        return true;
                    case R.id.setting:
                        startActivity(new Intent(getApplicationContext()
                                ,setting.class));
                        overridePendingTransition(0,0);
                        return true;

                }
                return false;
            }
        });
    }
}

menu_navigation.xml

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

    <item
        android:id="@+id/home"
        android:title="Home"
        android:icon="@drawable/ic_home"/>
    <item
        android:id="@+id/about"
        android:title="About"
        android:icon="@drawable/ic_about"/>
    <item
        android:id="@+id/test"
        android:title="Test"
        android:icon="@drawable/ic_test"/>
    <item
        android:id="@+id/term"
        android:title="Term"
        android:icon="@drawable/ic_term"/>
    <item
        android:id="@+id/setting"
        android:title="Setting"
        android:icon="@drawable/ic_setting"/>
</menu>