BottomNavigationView的基本使用

因爲最近自己在寫一個壁紙類的apk,需要底部的導航欄來配合fragment來使用,所以寫個筆記供讀者閱覽。
GIF.gif

1.簡介

BottomNavigationView是一個底部導航欄控件,一般和fragment一起使用。

2.導包

implementation 'com.google.android.material:material:1.0.0'

3.使用

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:itemTextColor="@drawable/main_bottom_navigation"
        app:itemIconTint="@drawable/main_bottom_navigation"
        app:menu="@menu/navigation">
    </android.support.design.widget.BottomNavigationView>

控件分析:
app:itemTextColor 指的是導航欄文字的顏色
app:itemIconTint 指的是導航欄中圖片的顏色
app:iteamBackground 指的是底部導航欄的背景顏色,默認是主題的顏色
app:menu 指的是佈局(文字和圖片都寫在這個裏面)

注意:關於app:的使用,都需要在最外層裏面加入下面這個
xmlns:app="http://schemas.android.com/apk/res-auto" 系統會默認導入的,如果沒有就手動導入。

4.navigation界面

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/main_wallpaper"
        android:icon="@drawable/ic_wallpaper"
        android:title="@string/main_wallpaper" />
    <item
        android:id="@+id/main_music"
        android:icon="@drawable/ic_music"
        android:title="@string/main_music" />
    <item
        android:id="@+id/main_my"
        android:icon="@drawable/ic_user"
        android:title="@string/main_user" />
</menu>

ic_開頭的是圖片
main_開頭的是文字

可以從icon和title來看出來,這裏我建議大家使用文字的時候儘量不要寫在xml裏面,而是寫在strings裏面,因爲我經常做多國語言的翻譯,有這個習慣。這個習慣會讓你以後對文字的優化,或者添加都有好的提升。

下面我們看控件點擊時候的顏色

5.main_bottom_navigation的界面

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#0015ff" android:state_checked="true"></item>
    <item android:color="#0084ff" android:state_checked="false"></item>
</selector>

item裏面有很多的屬性,例如focus,drawable。

6.完整的activity代碼

import android.os.Build;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

import bottomnavigationview.luo.com.bottomnavigationview02.fragment.MusicFragment;
import bottomnavigationview.luo.com.bottomnavigationview02.fragment.MyFragment;
import bottomnavigationview.luo.com.bottomnavigationview02.fragment.WallpaperFragment;

/**
 * Created by luo on 2019/7/3.
 */

public class MainActivity extends AppCompatActivity {

    private FrameLayout mainFrame;
    private BottomNavigationView bottomNavigation;
    private WallpaperFragment wallpaperFragment;
    private MusicFragment musicFragment;
    private MyFragment myFragment;
    private Fragment[] fragments;
    private int lastfragment = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setHalfTransparent();
        initView();
    }

    private void initView() {
        wallpaperFragment = new WallpaperFragment();
        musicFragment = new MusicFragment();
        myFragment = new MyFragment();
        fragments = new Fragment[]{wallpaperFragment, musicFragment, myFragment};
        mainFrame = (FrameLayout) findViewById(R.id.mainFrame);
        //設置fragment到佈局
        getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame, wallpaperFragment).show(wallpaperFragment).commit();

        bottomNavigation = (BottomNavigationView) findViewById(R.id.bottomNavigation);

        //這裏是bottomnavigationview的點擊事件  
        bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.main_wallpaper:
                  //這裏因爲需要對3個fragment進行切換
                  //start 
                    if (lastfragment != 0) {
                        switchFragment(lastfragment, 0);
                        lastfragment = 0;
                    }
                  //end    
                  //如果只是想測試按鈕點擊,不管fragment的切換,可以把start到end裏面的內容去掉
                    return true;
                case R.id.main_music:
                    if (lastfragment != 1) {
                        switchFragment(lastfragment, 1);
                        lastfragment = 1;
                    }
                    return true;
                case R.id.main_my:
                    if (lastfragment != 2) {
                        switchFragment(lastfragment, 2);
                        lastfragment = 2;
                    }
                    return true;
                default:
                    break;
            }
            return false;
        }
    };

    /**
    *切換fragment
    */
    private void switchFragment(int lastfragment, int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        //隱藏上個Fragment
        transaction.hide(fragments[lastfragment]);
        if (fragments[index].isAdded() == false) {
            transaction.add(R.id.mainFrame, fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
    }

    /**
     * 半透明狀態欄
     */
    protected void setHalfTransparent() {
        if (Build.VERSION.SDK_INT >= 21) {//21表示5.0
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else if (Build.VERSION.SDK_INT >= 19) {//19表示4.4
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //虛擬鍵盤也透明
            // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }
}

7.小結

1.BottomNavigationView需要導包,與appcompat一致
2.需要一個menu的xml,文字和圖片
3.按鈕的點擊設置

8.android studio自動創建BottomNavigationView

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