代码之家  ›  专栏  ›  技术社区  ›  TheSwiftGuy77

底部导航视图:在应用程序启动时更改默认高亮显示的选项卡

  •  3
  • TheSwiftGuy77  · 技术社区  · 9 年前

    My Navigation View

    如上所示,我有一个按钮导航视图,当我启动应用程序时,默认情况下,我的“类别”选项卡高亮显示,但我想在启动应用程序时高亮显示我的“主页”选项卡,有人能帮我摆脱它吗?

    3 回复  |  直到 9 年前
        1
  •  5
  •   AskNilesh    9 年前

    尝试此用法 setSelectedItemId() 您的方法 BottomNavigationView

    BottomNavigationView bottomNavigationView;
    bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
    bottomNavigationView.setSelectedItemId(R.id.home_menu);
    

    或使用 viewPager.setCurrentItem(); 像这样查看寻呼机

    viewPager.setCurrentItem(2);
    
        2
  •  1
  •   James Bond    4 年前

    您可以在 res/navigation/mobile_navigation.xml

    更改 app:startDestination="@+id/navigation_home"

    <?xml version="1.0" encoding="utf-8"?>
    <navigation 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/mobile_navigation"
        app:startDestination="@+id/navigation_home">
    
        <fragment
            android:id="@+id/navigation_home"
            android:name="com.github.bottomchan.ui.home.HomeFragment"
            android:label="@string/title_home"
            tools:layout="@layout/fragment_home" />
    
        <fragment
            android:id="@+id/navigation_dashboard"
            android:name="com.github.bottomchan.ui.dashboard.DashboardFragment"
            android:label="@string/title_dashboard"
            tools:layout="@layout/fragment_dashboard" />
    </navigation>
    
    
        3
  •  0
  •   yash786    9 年前
       /***Just try to use below code snipet***/
    
       viewPager.setCurrentItem(2);
    
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
       {
         home_icon.setImageDrawable(getResources().getDrawable(R.drawable.home_icon_selected_state, getApplicationContext().getTheme()));
    
       }
       else
       {
         home_icon.setImageDrawable(getResources().getDrawable(R.drawable.home_icon_selected_state));
       }