简体   繁体   English

使用底部导航栏

[英]Using bottom nav bar

I am trying to make a bottom navigation bar to navigation bar between activities but I find using fragments very confusing so I tried using but its not working tried to look for something that I can use but I did not find anything online so as people who have experience can I use bottom navigation bar without fragments. 我试图在活动之间创建一个底部导航栏,但我发现使用片段非常令人困惑,因此我尝试使用但无法正常工作,试图寻找可以使用的东西,但没有找到任何在线内容,例如经验,我可以使用没有碎片的底部导航栏。

My code: 我的代码:

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

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {


           case R.id.navigation_dashboard:

                Intent intent1 = new Intent(RedirectedsearchActivity.this, 
      SearchActivity.class);
                startActivity(intent1);
                return true;

It didn't work. 没用

You can try the navigation component. 您可以尝试导航组件。

With it, it is much easier to work with fragments. 使用它,可以更轻松地处理片段。 There are a couple of links to documentation and articles. 有两个指向文档和文章的链接。

Documentation https://developer.android.com/guide/navigation 文档https://developer.android.com/guide/navigation

And tutorial https://www.androidauthority.com/android-navigation-architecture-component-908660/ 和教程https://www.androidauthority.com/android-navigation-architecture-component-908660/

Try this code and make activity_main layout.. without fragment it not working that time you need each and every activity define bottom navigation. 尝试使用此代码并制作activity_main布局。没有片段,那一次您需要为每个活动定义底部导航都无法正常工作。

<?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:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent">

<include
    android:id="@+id/amToolbar"
    layout="@layout/app_toolbar"
    />

<FrameLayout
    android:id="@+id/activity_main_container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    app:layout_constraintTop_toBottomOf="@+id/amToolbar"
    app:layout_constraintBottom_toTopOf="@id/bottom_navigation"
     />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:itemIconTint="@drawable/bottom_navigation_colors"
    app:itemTextColor="@drawable/bottom_navigation_colors"
    app:labelVisibilityMode="labeled"
    app:layout_constraintBottom_toBottomOf="parent"
    app:menu="@menu/bottom_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

after that mainActivity call click event.. 在该mainActivity调用click事件之后。

    // Set action to perform when any menu-item is selected.
    bottom_navigation.setOnNavigationItemSelectedListener { item ->
        item.isChecked = true
        selectFragment(item)
        false
    }
private fun selectFragment(item: MenuItem?) {
    item?.isChecked = true
    when (item?.itemId) {
        R.id.action_uploadFile -> {
         // here define fragment change
        }

    }
}

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

相关问题 使用库中的片段活动使用底部 NAV 栏更改片段 - Changing fragment using bottom NAV bar using fragment activity in a library 底部导航栏片段多次 - bottom nav bar fragment multiple times 如何在底部导航栏中删除标题标题 - Android Studio - How to remove Header title in bottom nav bar - Android Studio 在 Android Studio 中将底部导航栏与其他代码合并 - Merging a Bottom Nav bar with other code in Android Studio 底部导航栏保持当前状态时如何启动不同的布局 - How to Launch different layouts while bottom nav bar stays present Android - 我的 Snackbar 出现在我的底部导航栏前面而不是上面,如何修复? - Android - My Snackbar appears in front of my Bottom Nav Bar instead above it, how to fix it? Android 底部导航活动 - Android Bottom Nav Activity 为底部导航栏保存片段的活动到活动永远不会到达片段,而是通过 FrameLayout 保存它们的活动 - Activity to activity that holds fragments for bottom nav bar never get to fragment but to the activity that hold them by the FrameLayout 使用ExpandableListView时,在屏幕底部放置一个菜单栏 - Putting a menu bar on the bottom of the screen while using ExpandableListView 使用片段时 Android Java 底部导航栏不可见 - Android Java Bottom navigation bar not visible when using fragments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM