如何在Android中更改底部导航布局中的主菜单

时间:2018-02-23 09:59:23

标签: android android-layout android-fragments bottomnavigationview

我在BottomNavigationLayout

中添加了三个菜单

如何在启动时默认打开中心菜单?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);
    loadFragment(new ProfileFragment());

    BottomNavigationView navigation = findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

private void loadFragment(Fragment fragment) {
    // load fragment
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragmentContainer, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

我用它来加载片段,它还加载与中心菜单相关的片段,但在底部导航中选择了第一个菜单。

我希望你理解我的问题。如果没有,那么检查冲突皇家。在这个游戏中,战斗布局是启动时的第一个弹出窗口以及在底部导航中选择的战斗菜单。

如果有人知道怎么做,请帮助我。 提前谢谢

4 个答案:

答案 0 :(得分:0)

onCreate()方法内部调用您要在应用启动时首先打开的片段。

当您点击底部菜单时,您正在执行一些操作以更改带有片段的菜单颜色,将该片段放在活动的onCreate上并执行操作,因此默认情况下,当应用启动时,它会显示您的想要的片段。

以下是参考资料:

 HomeFragment homeFragment = new HomeFragment().newInstance();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragmentContainer, homeFragment)
                .commit();

完整代码:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);
    loadFragment(new ProfileFragment());

    BottomNavigationView navigation = findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

     HomeFragment homeFragment = new HomeFragment().newInstance();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragmentContainer, homeFragment)
                .commit();

}

答案 1 :(得分:0)

在您的Mainactivity中添加此内容

public boolean check(int one,int two){
    while(one>0){
        if(one%10>two)
            return false;
        one=one/10;
    }
    return true;
}

答案 2 :(得分:0)

将中间标签设为选定项目

bottomNavigationView.setSelectedItemId(R.id.tab2);

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.tab1:
                loadFragment(new ProfileFragment1());
                return true;
            case R.id.tab2:
                loadFragment(new ProfileFragment2());
                return true;
            case R.id.tab3:
                loadFragment(new ProfileFragment3());
                return true;
        }
        return false;
    }
};

答案 3 :(得分:0)

刚刚去找这个伴侣!只需复制并粘贴到您的编辑器中就可以了!

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);
 //   loadFragment(new ProfileFragment()); Removed this line

    BottomNavigationView navigation = findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
// Added this line
navigation.setSelectedItemId(bottomNavigation.getMenu().getItem(1).getItemId()); 
}

private void loadFragment(Fragment fragment) {
    // load fragment
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragmentContainer, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
}