最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android底部導(dǎo)航組件BottomNavigationView

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

什么是BottomNavigationView

底部菜單欄

BottomNavigationView的簡(jiǎn)單用法

需求:如上圖所示。點(diǎn)擊測(cè)試一菜單,展示test1fragment。點(diǎn)擊測(cè)試二菜單,展示test2fragment。點(diǎn)擊測(cè)試三菜單,展示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="測(cè)試一" />
  <item
    android:id="@+id/navigation_test2"
    android:icon="@drawable/ic_dashboard_black_24dp"
    android:title="測(cè)試二" />
  <item
    android:id="@+id/navigation_test3"
    android:icon="@drawable/ic_notifications_black_24dp"
    android:title="測(cè)試三" />
</menu>

第三步,書寫testActivity文件。重點(diǎn)是setOnNavigationItemSelectedListener點(diǎn)擊事件

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 點(diǎn)擊事件
    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();
    }
  }
}

到此這篇關(guān)于Android底部導(dǎo)航組件BottomNavigationView的文章就介紹到這了,更多相關(guān)Android BottomNavigationView內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

中西区| 宁夏| 云林县| 栖霞市| 宁强县| 东源县| 衡阳市| 湘阴县| 拜城县| 漳州市| 原平市| 金堂县| 太原市| 庆元县| 延安市| 福海县| 奎屯市| 翼城县| 塔城市| 张家港市| 五常市| 钟山县| 万盛区| 湘潭县| 加查县| 宁陕县| 梧州市| 霞浦县| 新宾| 莎车县| 黄山市| 民乐县| 台南市| 论坛| 镇平县| 塘沽区| 固镇县| 宁德市| 新竹市| 武乡县| 灵山县|