欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android底部導航組件BottomNavigationView

 更新時間:2023年03月20日 10:20:48   作者:Dormiveglia-flx  
這篇文章主要介紹了Android底部導航組件BottomNavigationView,BottomNavigationView是相當于一個導航的標簽,但是它的形式就是像QQ微信之類的界面,至于寫出后怎樣綁定這三個界面,就得用Fragment,寫這三個頁面的布局

什么是BottomNavigationView

底部菜單欄

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的文章就介紹到這了,更多相關Android BottomNavigationView內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Android實現(xiàn)鬧鐘功能小Dome

    Android實現(xiàn)鬧鐘功能小Dome

    本篇文章主要介紹了Android實現(xiàn)鬧鐘功能小Dome,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • Kotlin Thread線程與UI更新詳解

    Kotlin Thread線程與UI更新詳解

    本篇主要介紹Kotlin中Thread線程與UI更新,注意不是協(xié)程而是線程。Kotlin本身是支持線程的。同時協(xié)程也是運行在線程中的
    2022-12-12
  • Android ADB超簡單的安裝教程(推薦)

    Android ADB超簡單的安裝教程(推薦)

    adb是Android的一個很重要的調(diào)試工具,熟練掌握后可實現(xiàn)很多功能,比如有些手機的解鎖、ROOT就會用到adb工具??珊芏嗯笥讯颊f不會安裝,所以下面這篇文章主要給大家介紹了關于Android ADB超簡單的安裝教程,安裝非常簡單,需要的朋友可以參考下
    2018-07-07
  • Android使用kotlin實現(xiàn)多行文本上下滾動播放

    Android使用kotlin實現(xiàn)多行文本上下滾動播放

    這篇文章主要為大家詳細介紹了Android使用kotlin實現(xiàn)多行文本的上下滾動播放,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • android不同activity之間共享數(shù)據(jù)解決方法

    android不同activity之間共享數(shù)據(jù)解決方法

    最近做局域網(wǎng)socket連接問題,要在多個activity之間公用一個socket連接,就在網(wǎng)上搜了下資料,感覺還是application方法好用,帖出來需要的朋友可以參考下
    2012-11-11
  • Android實現(xiàn)環(huán)信修改頭像和昵稱

    Android實現(xiàn)環(huán)信修改頭像和昵稱

    這篇文章主要為大家詳細介紹了Android實現(xiàn)環(huán)信修改頭像和昵稱,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • android開發(fā)教程之自定義屬性用法詳解

    android開發(fā)教程之自定義屬性用法詳解

    這篇文章主要介紹了android開發(fā)中的自定義屬性用法詳解,需要的朋友可以參考下
    2014-04-04
  • Android Service總結(jié)及詳細介紹

    Android Service總結(jié)及詳細介紹

    本文主要介紹Android Service的知識,這里整理了詳細資料及簡單實現(xiàn)示例代碼,有需要的小伙伴可以參考下
    2016-09-09
  • 2021最新Android筆試題總結(jié)美團Android崗職能要求

    2021最新Android筆試題總結(jié)美團Android崗職能要求

    這篇文章主要介紹了2021最新Android筆試題總結(jié)以及美團Android崗職能要求,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • Android 深入探究自定義view之事件的分發(fā)機制與處理詳解

    Android 深入探究自定義view之事件的分發(fā)機制與處理詳解

    對于安卓程序員來說,自定義view簡直不要太重要,畢竟有很多功能,譬如圓形頭像這些,用單純的原生非常難以實現(xiàn),而用自定義view,簡直分分鐘
    2021-11-11

最新評論