溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android底部導航組件BottomNavigationView怎么使用

發(fā)布時間:2023-03-20 14:27:27 來源:億速云 閱讀:294 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“Android底部導航組件BottomNavigationView怎么使用”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

BottomNavigationView的簡單用法

需求:如上圖所示。點擊測試一菜單,展示test1fragment。點擊測試二菜單,展示test2fragment。點擊測試三菜單,展示test3fragment。

第一步,testActivity布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  // 容器,承載fragment
  <FrameLayout
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />
  // BottomNavigationView
  <com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/windowBackground"
    app:menu="@menu/bottom_nav_menu_test" />
</LinearLayout>

第二步,寫B(tài)ottomNavigationView所需要的菜單

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:id="@+id/navigation_test1"
    android:icon="@drawable/ic_home_black_24dp"
    android:title="測試一" />
  <item
    android:id="@+id/navigation_test2"
    android:icon="@drawable/ic_dashboard_black_24dp"
    android:title="測試二" />
  <item
    android:id="@+id/navigation_test3"
    android:icon="@drawable/ic_notifications_black_24dp"
    android:title="測試三" />
</menu>

第三步,書寫testActivity文件。重點是setOnNavigationItemSelectedListener點擊事件

public class TestActivity extends AppCompatActivity {
  List<Fragment> mFragments = new ArrayList<>();
  test1Fragment t1f = new test1Fragment();
  test2Fragment t2f = new test2Fragment();
  test3Fragment t3f = new test3Fragment();
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    BottomNavigationView navView = findViewById(R.id.nav_view);
    mFragments.add(t1f);
    mFragments.add(t2f);
    mFragments.add(t3f);
    // navView 點擊事件
    navView.setOnNavigationItemSelectedListener((item)->{
      switchFragment(item.getItemId());
      return true;
    });
  }
  private void switchFragment(int id) {
    Fragment fragment = null;
    switch (id) {
      case R.id.navigation_test1:
        fragment = mFragments.get(0);
        break;
      case R.id.navigation_test2:
        fragment = mFragments.get(1);
        break;
      case R.id.navigation_test3:
        fragment = mFragments.get(2);
        break;
      default:
        break;
    }
    if (fragment != null) {
      getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,fragment).commit();
    }
  }
}

“Android底部導航組件BottomNavigationView怎么使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI