利用BottomNavigationView實現底部標籤欄

在Android Studio上創建官方默認的首屏標籤頁面很方便,首先右擊需要添加標籤欄的模塊,在彈出的右鍵菜單中依次選擇“New”——“Activity”——“Bottom Navigation Activity”,彈出下圖所示的活動創建頁面。


在創建頁面的“Activity Name”一欄填寫新活動的名稱,再單擊頁面右下角的Finish按鈕,Android Studio就會自動創建該活動的Java代碼及其佈局文件。
然後編譯運行App,進入剛創建的活動頁面,其界面效果如下圖所示。可見測試頁面的底部默認提供了三個導航標籤,分別是Home、Dashboard和Notifications。


注意到初始頁面的Home標籤從文字到圖片均爲高亮顯示,說明當前處於Home頻道。接着點擊Dashboard標籤,此時界面如下圖所示,可見切換到了Dashboard頻道。


繼續點擊Notifications,此時界面如下圖所示,可見切換到了Notifications頻道。


不過爲了定製頁面的詳細內容,開發者仍需修改相關代碼,譬如將標籤文字從英文改成中文,將頻道上方的描述說明從英文改成中文,給頻道頁面添加圖像視圖等其他控件等等,故而還得梳理標籤欄框架的實現方式。
首先查看標籤頁面的佈局文件,它的關鍵代碼如下所示:

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/bottom_nav_menu" />    <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_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

從佈局內容可知,標籤頁面主要包含兩個組成部分,一個是位於底部的BottomNavigationView(底部導航視圖),另一個是位於其上佔據剩餘屏幕的碎片fragment。底部導航視圖又由一排標籤菜單組成,具體菜單在@menu/bottom_nav_menu中定義;而碎片爲各頻道的主體部分,具體內容在app:navGraph="@navigation/mobile_navigation中定義。喲,原來奧妙就在這兩個文件當中,趕緊打開menu目錄之下的bottom_nav_menu.xml看看:

<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="@string/title_home" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/title_dashboard" />    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_notifications" />
</menu>

上面的菜單定義文件以menu爲根節點,內部容納三個item節點,分別對應屏幕底部的三個標籤。每個item節點都擁有id、icon、title三個屬性,其中id指定該菜單項的編號,icon指定該菜單項的圖標,title指定該菜單項的文本。順藤摸瓜查看values目錄之下的strings.xml,果然找到了下面的三個標籤文本定義:

    <string name="title_home">Home</string>
    <string name="title_dashboard">Dashboard</string>
    <string name="title_notifications">Notifications</string>

搞清楚了底部標籤欄的資源情況,接着打開navigation目錄之下的mobile_navigation.xml,究竟裏面是怎麼定義各個頻道的呢?

<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/mobile_navigation"
    app:startDestination="@+id/navigation_home">

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.example.chapter12.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="com.example.chapter12.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />

    <fragment
        android:id="@+id/navigation_notifications"
        android:name="com.example.chapter12.ui.notifications.NotificationsFragment"
        android:label="@string/title_notifications"
        tools:layout="@layout/fragment_notifications" />
</navigation>

上述的導航定義文件以navigation爲根節點,內部依舊分佈着三個fragment節點,顯然正好對應三個頻道。每個fragment節點擁有id、name、label、layout四個屬性,各屬性的用途說明如下:
id:指定當前碎片的編號。
name:指定當前碎片的完整類名路徑。
label:指定當前碎片的的標題文本。
layout:指定當前碎片的佈局文件。
這些默認的碎片代碼到底有何不同,打開其中一個HomeFragment.java研究研究,它的關鍵代碼如下所示:

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        final TextView textView = root.findViewById(R.id.text_home);
        homeViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        return root;
    }

看來頻道用到的碎片代碼仍然在onCreateView方法中根據佈局文件生成頁面元素,這樣修改頻道界面就變成給碎片編碼了。總算理清了這種底部導航的實現方式,接下來準備修理修理默認的標籤及其頻道。先打開values目錄之下的strings.xml,把三個標籤的文字從英文改成中文,修改內容示例如下:

    <string name="title_home">首頁</string>
    <string name="title_dashboard">儀表盤</string>
    <string name="title_notifications">消息</string>

再打開三個頻道的碎片代碼,給文本視圖填上中文描述,首頁頻道HomeFragment.java的修改內容示例如下:

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        final TextView textView = root.findViewById(R.id.text_home);
        textView.setText("這是首頁頁面");
        return root;
    }

因爲默認代碼裏的ViewModel並非必需組件,所以簡潔起見省去了ViewModel相關代碼,另外兩個碎片頻道的代碼依此類推。
重新編譯運行App,改過的各頻道界面如下面各圖所示,從上到下分別爲首頁頻道、儀表盤頻道、消息頻道的頁面效果,可見三個頻道從標籤文本和說明描述都改成了漢字。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章