简体   繁体   English

如何动态更改底部导航项文本?

[英]How can i dynamically change bottom navigation items text?

在此处输入图片说明

Im trying to achieve the above design.我试图实现上述设计。 I'm able to change the icons, but cannot change the bottom text.我可以更改图标,但无法更改底部文本。 Im using material bottom navigation bar我正在使用材料底部导航栏

I achieved the icon change by implementing separate selector drawables for each icon.我通过为每个图标实现单独的选择器可绘制来实现图标更改。 Is there any way that i can do the same with the nav titles?有什么办法可以对导航标题做同样的事情吗?

MainActivity.class主Activity.class

public class MainActivity extends AppCompatActivity {
BottomNavigationView navView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    navView = findViewById(R.id.nav_view);

    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
            R.id.navigation_home, R.id.navigation_saved, R.id.navigation_applied, R.id.navigation_profile, R.id.navigation_more)
            .build();

    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
    NavigationUI.setupWithNavController(navView, navController);



}

activity_main.xml活动_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        app:itemIconTint="@null"
        app:itemHorizontalTranslationEnabled="false"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:labelVisibilityMode="labeled"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

bottom_nav_menu.xml bottom_nav_menu.xml

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/home_selector"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_saved"
        android:icon="@drawable/saved_jobs_selector"
        android:title="@string/title_saved" />

    <item
        android:id="@+id/navigation_applied"
        android:icon="@drawable/applied_jobs_selector"
        android:title="@string/title_applied" />

    <item
        android:id="@+id/navigation_profile"
        android:icon="@drawable/profile_selector"
        android:title="@string/title_profile" />


    <item
        android:id="@+id/navigation_more"
        android:icon="@drawable/more_selector"
        android:title="@string/title_More" />

</menu>

Hi I could change the title in the navigation bar by implementing:嗨,我可以通过执行以下操作来更改导航栏中的标题:

private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item)
    {
        switch (item.getItemId())
        {
            
        }
    }
};

After it in the switch you add the string based on the item id在 switch 之后,您添加基于项目 id 的字符串

You can change like this:你可以这样改变:

for(i in 0 until bottomNavigation.maxItemCount){
   bottomNavigation.menu[i].title = "My Custom Title"
}

I change the title and Icon programmatically like that:我像这样以编程方式更改标题和图标:

fun changeMenuItemTextAndIcon(){
    val item: MenuItem = bottom_navigation.menu.findItem(R.id.action_scan)
    item.title = getString(R.string.label_scan_item)
    item.icon = ContextCompat.getDrawable(activity, R.drawable.ic_barcode)
}

检查代码行下方,并在您在 Kotlin 中选择 BottomNavigation 项时添加它(将 java 转换为 kotlin )。

navView.getCheckedItem().setTitle(".");

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

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