Android以编程方式更改导航抽屉菜单项文本

Posted

技术标签:

【中文标题】Android以编程方式更改导航抽屉菜单项文本【英文标题】:Android Change Navigation Drawer Menu Items Text programmatically 【发布时间】:2016-01-21 22:55:49 【问题描述】:

我的应用中有新的导航抽屉,我想从代码中动态更改导航视图菜单项的标题文本。我看过很多帖子,但我不知道,我该怎么做。我怎样才能正确地做到这一点?

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) 
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() 
    @Override
    public void onClick(View view) 
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();
    
);

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);


activity_main_drawer.xml

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

<group android:checkableBehavior="single">
    <item android:id="@+id/nav_camara" android:icon="@android:drawable/ic_menu_camera"
        android:title="Import" />
    <item android:id="@+id/nav_gallery" android:icon="@android:drawable/ic_menu_gallery"
        android:title="Gallery" />
    <item android:id="@+id/nav_slideshow" android:icon="@android:drawable/ic_menu_slideshow"
        android:title="Slideshow" />
    <item android:id="@+id/nav_manage" android:icon="@android:drawable/ic_menu_manage"
        android:title="Tools" />
</group>

<item android:title="Communicate">
    <menu>
        <item android:id="@+id/nav_share" android:icon="@android:drawable/ic_menu_share"
            android:title="Share" />
        <item android:id="@+id/nav_send" android:icon="@android:drawable/ic_menu_send"
            android:title="Send" />
    </menu>
</item>

【问题讨论】:

动态更改菜单的唯一方法是清除它并加载另一个菜单:***.com/questions/31203212/… 【参考方案1】:

对于 Kotliin:

val bottomNavigationView = findViewById<BottomNavigationView(R.id.bottom_nav)
        
val menu: Menu = bottomNavigationView.menu
val secondMenu: MenuItem = menu.findItem(R.id.needsFragment)
        // To set menu title according to user type (ie. Abc / Xyz )
        when(userType)
            1 -> secondMenu.title = "Abc"
            2 -> secondMenu.title = "Xyz"
        
        bottomNavigationView.setupWithNavController(navController)
        bottomNavigationView.setOnNavigationItemReselectedListener  

【讨论】:

【参考方案2】:

如果您在导航抽屉菜单中有一个布局作为项目并且您想以编程方式更改它,此代码 sn-p 将帮助您:

MenuItem nav = navigationView.getMenu().findItem(R.id.nav_close_app);
nav.setActionView(R.layout.item_navigationdrawer_close_app);

【讨论】:

【参考方案3】:

此代码可用于动态添加菜单。它对我有用......代码的主要部分是 displayItems();.....将项目的 String[] 作为参数传递给函数......它可以工作。

public class Courses extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener 

    TextView nn,ne,np,nl;
    SharedPreferences sp;
    NavigationView navigationView;
    DrawerLayout drawer;
    ImageView img;
    NavigationView nv;
    int id;

String[] acc="ACPFAT","CPFA","Tally.ERP 9 Simplified","D P F A","CA Articles","DAA","TFAP","Tally.ERP 9 - Specialization","GST (Goods and service Tax)";

drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.setDrawerTitle(Gravity.CENTER,n);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, 
                 R.string.navigation_drawer_open,R.string.navigation_drawer_close);

drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);

Intent i=getIntent();
id=i.getIntExtra("button",0);

switch(id)
    

        case R.id.account:
            img.setImageResource(R.drawable.acc);
            displayItems(acc);
            break;

        case R.id.modular:
            img.setImageResource(R.drawable.acc);
            displayItems(mod);
            break;

        case R.id.diploma:

            img.setImageResource(R.drawable.acc);
            displayItems(dc);
            break;

    

public void displayItems(String[] a)
  

    final NavigationView navigationView=findViewById(R.id.nav_view);
    final DrawerLayout drawer=findViewById(R.id.drawer_layout);
    Menu menu=navigationView.getMenu();
    SubMenu sb=menu.addSubMenu("Courses");

    int i=0;
    while(i<a.length)
    
        sb.add(a[i]);
        i++;
    
 

【讨论】:

【参考方案4】:
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menus);


    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    Menu menu = navigationView.getMenu();
    MenuItem nav_login = menu.findItem(R.id.nav_logout);
nav_login.setTitle("Login");



这是正确的!希望一切顺利......

【讨论】:

【参考方案5】:

如果您从导航菜单抽屉列表中重命名或删除项目,请更改如下代码

  NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    if (navigationView != null) 
        Menu menu = navigationView.getMenu();
        menu.findItem(R.id.nav_profile).setTitle("My Account");
        menu.findItem(R.id.nav_mng_task).setTitle("Control Task");
        //menu.findItem(R.id.nav_pkg_manage).setVisible(false);//In case you want to remove menu item
        navigationView.setNavigationItemSelectedListener(this);
    

【讨论】:

【参考方案6】:

您可以通过在MainActivity.java 文件中添加以下行以编程方式更改导航菜单项的标题。

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    //other stuff here
    ...
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

    // get menu from navigationView
    Menu menu = navigationView.getMenu();

    // find MenuItem you want to change
    MenuItem nav_camara = menu.findItem(R.id.nav_camara);

    // set new title to the MenuItem
    nav_camara.setTitle("NewTitleForCamera");

    // do the same for other MenuItems
    MenuItem nav_gallery = menu.findItem(R.id.nav_gallery);
    nav_gallery.setTitle("NewTitleForGallery");

    // add NavigationItemSelectedListener to check the navigation clicks
    navigationView.setNavigationItemSelectedListener(this);


这对我来说很好。希望对你有帮助。

【讨论】:

以上是关于Android以编程方式更改导航抽屉菜单项文本的主要内容,如果未能解决你的问题,请参考以下文章

Android中的导航抽屉菜单项标题颜色

隐藏导航抽屉菜单项 - Android

android:以编程方式更改选项菜单项

Android - 导航抽屉 - 与动态菜单项重叠的片段

如何在导航抽屉中显示菜单类别的菜单图标?

Android:更改导航抽屉应用程序中的当前内容,而不在抽屉菜单中显示项目