Navigation學(xué)習(xí)【三】——BottomNavigationView的簡單使用

借助Android Studio新建項(xiàng)目時(shí)的Bottom Navigation Activity模板以及官方文檔實(shí)現(xiàn)BottomNavigationView的簡單使用佛致。

Demo鏈接

簡單使用

  1. 首先引入Navigation:

    dependencies {
         ...
         
         //navigation
         implementation 'androidx.navigation:navigation-fragment:2.2.0'
         implementation 'androidx.navigation:navigation-ui:2.2.0'
     }
    
  2. 創(chuàng)建對應(yīng)的navigation文件nav_graph.xml

    <?xml version="1.0" encoding="utf-8"?>
    <navigation xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/nav_graph"
        app:startDestination="@id/homeFragment">
    
       <fragment
           android:id="@+id/navigation_home"
           android:name="com.example.bottomnavigationtest.ui.HomeFragment"
           android:label="HomeFragment"
           tools:layout="@layout/fragment_test" />
        
       <fragment
           android:id="@+id/navigation_dashboard"
           android:name="com.example.bottomnavigationtest.ui.DashboardFragment"
           android:label="DashboardFragment" />
        
       <fragment
           android:id="@+id/navigation_notifications"
           android:name="com.example.bottomnavigationtest.ui.NotificationFragment"
           android:label="NotificationFragment" />
    </navigation>
    
  3. 創(chuàng)建底部導(dǎo)航欄按鈕文件bottom_nav_menu.xml陌凳,注意這里的id要與 nav_graph.xml對應(yīng)的fragment一致

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:id="@+id/navigation_home"
            android:icon="@drawable/ic_home_black_24dp"
            android:title="Home" />
    
        <item
            android:id="@+id/navigation_dashboard"
            android:icon="@drawable/ic_dashboard_black_24dp"
            android:title="Dashboard" />
    
        <item
            android:id="@+id/navigation_notifications"
            android:icon="@drawable/ic_notifications_black_24dp"
            android:title="Notifications" />
    
    </menu>
    
  4. 創(chuàng)建相應(yīng)MainActivity布局 activity_main.xml

     <?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"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context=".MainActivity">
     
         <!-- app:navGraph="@navigation/nav_graph" 關(guān)聯(lián)navigation導(dǎo)航 -->
         <fragment
             android:id="@+id/nav_host_fragment"
             android:name="androidx.navigation.fragment.NavHostFragment"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             app:defaultNavHost="true"
             app:layout_constraintBottom_toTopOf="@id/nav_view"
             app:layout_constraintLeft_toLeftOf="parent"
             app:layout_constraintRight_toRightOf="parent"
             app:layout_constraintTop_toTopOf="parent"
             app:navGraph="@navigation/nav_graph" />
     
        <!-- app:menu="@menu/bottom_nav_menu" 關(guān)聯(lián)底部導(dǎo)航item -->
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/bottom_nav_menu" />
     
     </androidx.constraintlayout.widget.ConstraintLayout>
    
  5. NavigayionNavController相關(guān)聯(lián)

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //BottomNavigationView
            BottomNavigationView navigationView = findViewById(R.id.nav_view);
            //構(gòu)建導(dǎo)航id  menu 與 navigation中的id要相對應(yīng)
            AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                    R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                    .build();
            final NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
            //設(shè)置ActionBar標(biāo)題與當(dāng)前所顯示的fragment相對應(yīng)
            NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
            //與navcontroller相關(guān)聯(lián)
            NavigationUI.setupWithNavController(navigationView, navController);
    
            //設(shè)置BottomNavigationView點(diǎn)擊事件
            navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    //跳轉(zhuǎn)相應(yīng)fragment
                    navController.navigate(menuItem.getItemId());
                    //返回false會(huì)有一個(gè)點(diǎn)擊懸浮效果,返回true則不會(huì)有該效果
                    return false;
                }
            });
        }
    }
    
    

參考文章

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子纳鼎,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件贱鄙,死亡現(xiàn)場離奇詭異劝贸,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)逗宁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門悬荣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人疙剑,你說我怎么就攤上這事〖” “怎么了言缤?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長禁灼。 經(jīng)常有香客問我管挟,道長,這世上最難降的妖魔是什么弄捕? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任僻孝,我火速辦了婚禮,結(jié)果婚禮上守谓,老公的妹妹穿的比我還像新娘穿铆。我一直安慰自己,他們只是感情好斋荞,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布荞雏。 她就那樣靜靜地躺著,像睡著了一般平酿。 火紅的嫁衣襯著肌膚如雪凤优。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天蜈彼,我揣著相機(jī)與錄音筑辨,去河邊找鬼。 笑死幸逆,一個(gè)胖子當(dāng)著我的面吹牛棍辕,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播还绘,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼痢毒,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蚕甥?” 一聲冷哼從身側(cè)響起哪替,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎菇怀,沒想到半個(gè)月后凭舶,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體晌块,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年帅霜,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了匆背。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡身冀,死狀恐怖钝尸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情搂根,我是刑警寧澤珍促,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站剩愧,受9級特大地震影響猪叙,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜仁卷,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一穴翩、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧锦积,春花似錦芒帕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至基矮,卻和暖如春淆储,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背家浇。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工本砰, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人钢悲。 一個(gè)月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓点额,卻偏偏與公主長得像,于是被迫代替她去往敵國和親莺琳。 傳聞我的和親對象是個(gè)殘疾皇子还棱,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354