簡體   English   中英

使用底部導航欄

[英]Using bottom nav bar

我試圖在活動之間創建一個底部導航欄,但我發現使用片段非常令人困惑,因此我嘗試使用但無法正常工作,試圖尋找可以使用的東西,但沒有找到任何在線內容,例如經驗,我可以使用沒有碎片的底部導航欄。

我的代碼:

   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;

沒用

您可以嘗試導航組件。

使用它,可以更輕松地處理片段。 有兩個指向文檔和文章的鏈接。

文檔https://developer.android.com/guide/navigation

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

嘗試使用此代碼並制作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>

在該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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM