[英]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.