简体   繁体   English

顶部导航栏标题在 Android Studio 中替换

[英]Top Navigation bar Title replace in Android Studio

I know this is a common question to ask but I tried different ways to replace the title of the navigation bar but it didn't work for me.我知道这是一个常见的问题,但我尝试了不同的方法来替换导航栏的标题,但它对我不起作用。 I just want to replace the title of the navbar whenever I use toolbar.每当我使用工具栏时,我只想替换导航栏的标题。 Since the default title of my app_name is OldTitle.由于我的 app_name 的默认标题是 OldTitle。

在此处输入图像描述

What I've been tried but it's not working我已经尝试过但没有用

android:label="new title" 

I tried also this but it crashes my app我也试过这个但是它崩溃了我的应用程序

getSupportActionBar().setTitle("New Title");

error:错误:

Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference

I used menu folder and display in Tool Bar layout我使用menu文件夹并在工具栏布局中显示

my main_menu我的主菜单

<?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:id="@+id/app_bar_search"
    android:icon="@drawable/ic_search_black_24dp"
    android:title="Search"
    android:label="My new title"
    app:actionViewClass="android.widget.SearchView"
    app:showAsAction="always" />
</menu>

activitymain.xml活动main.xml

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/mainToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:backgroundTint="@android:color/white"
        app:liftOnScroll="true"/>

</com.google.android.material.appbar.AppBarLayout>

My Activity class我的活动 class

    Toolbar mToolbars = findViewById(R.id.mainToolbar);
    setSupportActionBar(mToolbars);

Try below code试试下面的代码

Java: Java:

Toolbar mToolbars = findViewById(R.id.mainToolbar);
setSupportActionBar(mToolbars);
getSupportActionBar().setTitle("New Title");

Kotlin: Kotlin:

  val mToolbars: Toolbar = findViewById(R.id.mainToolbar)
  setSupportActionBar(mToolbars)
  supportActionBar!!.setTitle("New Title")
getSupportActionBar().setTitle("New Title");

This line will give null error as you have to use setSupportActionBar() first, then you can use getSupportActionBar() for getting the toolbar.此行将给出 null 错误,因为您必须先使用 setSupportActionBar(),然后您可以使用 getSupportActionBar() 获取工具栏。 As for changing toolbar title on every navigation menu item click, you have to impelement NavigationView.OnNavigationItemSelectedListener like this.至于在每次单击导航菜单项时更改工具栏标题,您必须像这样实现 NavigationView.OnNavigationItemSelectedListener。

 Toolbar mToolbars = findViewById(R.id.mainToolbar);
 setSupportActionBar(mToolbars);
    
    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.nav_dashboard:
                    getSupportActionBar().setTitle("Dashboard");
                    break;
            }

            // to close the drawer layout on every click
            DrawerLayout drawer = findViewById(R.id.drawer_layout); 
            drawer.closeDrawer(GravityCompat.START); 

            return true;
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM