android.support.design.widget.BottomNavigationView+fragment

效果圖


Screenshot_20180102-154959.png

首先BottomNavigationView的使用是需要添加依賴包的

implementation 'com.android.support:design:26.1.0'

具體添加什么版本的依賴包奠骄,你可以手動點擊new->Activity->Bottom Navigation Activity系統(tǒng)會自動導(dǎo)入
1536659064(1).jpg

app:itemBackground="@color/colorPrimaryDark" --背景顏色
app:itemIconTint="@color/colorAccent" --圖標顏色
app:itemTextColor="@color/colorAccent" --字體顏色
圖標和按鈕的顏色可以使用selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@color/colorAccent"></item>
    <item android:color="@color/colorPrimary"></item>
</selector>

或者代碼:

navigation.itemIconTintList = resources.getColorStateList(R.drawable.text_color, null);
    navigation.itemTextColor = resources.getColorStateList(R.drawable.text_color, null);

布局main.xml文件

    <LinearLayout 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="android.my.com.myandroid.MainActivity"
android:orientation="vertical">

<FrameLayout
    android:id="@+id/framelayout_main"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    app:layout_constraintBottom_toTopOf="@id/bottom_nav" />
    <!--底部導(dǎo)航欄高度默認是 56dp-->
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:overScrollMode="never"
        android:scrollbars="none"
        app:itemBackground="@color/colorPrimaryDark"
        app:itemIconTint="@color/colorAccent"
        app:itemTextColor="@color/colorAccent"
        app:menu="@menu/navigation">
    </android.support.design.widget.BottomNavigationView>
    </LinearLayout>

按鈕布局在menu下navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_1"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="第一個" />

    <item
        android:id="@+id/navigation_2"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="第二個" />

    <item
        android:id="@+id/navigation_3"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="第三個" />

    <item
        android:id="@+id/navigation_4"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="第四個" />

    <item
        android:id="@+id/navigation_5"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="第五個" />

</menu>

通過mOnNavigationItemSelectedListener去監(jiān)聽按鈕點擊切換fragment

BottomNavigationView navigation;
    private FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction;
    Fragment1 fragment1;
    Fragment2 fragment2;
    Fragment3 fragment3;
    Fragment4 fragment4;
    Fragment5 fragment5;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_1:
                    setMyFragment(1);
                    break;
                case R.id.navigation_2:
                    setMyFragment(2);
                    break;
                case R.id.navigation_3:
                    setMyFragment(3);
                    break;
                case R.id.navigation_4:
                    setMyFragment(4);
                    break;
                case R.id.navigation_5:
                    setMyFragment(5);
                    break;
            }
            // true 會顯示這個Item被選中的效果 false 則不會
            return true;
        }
    };

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

        navigation =  findViewById(R.id.bottom_nav);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        disableShiftMode(navigation);//去除動畫
        navigation.setSelectedItemId(navigation.getMenu().getItem(0).getItemId());//默認選擇
        setMyFragment(1);//默認選擇
    }

    /**
     * 將所有的Fragment都置為隱藏狀態(tài)。
     *
     * @param transaction 用于對Fragment執(zhí)行操作的事務(wù)
     */
    private void hideFragments(FragmentTransaction transaction) {

        if (fragment1 != null) {
            transaction.hide(fragment1);
        }
        if (fragment2 != null) {
            transaction.hide(fragment2);
        }
        if (fragment3 != null) {
            transaction.hide(fragment3);
        }
        if (fragment4 != null) {
            transaction.hide(fragment4);
        }
        if (fragment5 != null) {
            transaction.hide(fragment5);
        }
    }

    private void setMyFragment(int Index){
        transaction = manager.beginTransaction();
        hideFragments(transaction);
        switch (Index){
            case 1:
                if (fragment1 == null) {
                    fragment1 = new Fragment1();
                    transaction.add(R.id.framelayout_main, fragment1);
                } else {
                    transaction.show(fragment1);
                }
                break;
            case 2:
                if (fragment2 == null) {
                    fragment2 = new Fragment2();
                    transaction.add(R.id.framelayout_main, fragment2);
                } else {
                    transaction.show(fragment2);
                }
                break;
            case 3:
                if (fragment3 == null) {
                    fragment3 = new Fragment3();
                    transaction.add(R.id.framelayout_main, fragment3);
                } else {
                    transaction.show(fragment3);
                }
                break;
            case 4:
                if (fragment4 == null) {
                    fragment4 = new Fragment4();
                    transaction.add(R.id.framelayout_main, fragment4);
                } else {
                    transaction.show(fragment4);
                }
                break;
            case 5:
                if (fragment5 == null) {
                    fragment5 = new Fragment5();
                    transaction.add(R.id.framelayout_main, fragment5);
                } else {
                    transaction.show(fragment5);
                }
                break;
        }
        transaction.commit();
    }
    @SuppressLint("RestrictedApi")
    public static void disableShiftMode(BottomNavigationView view) {
        //獲取子View BottomNavigationMenuView的對象
        BottomNavigationMenuView menuView;
        menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try {
            //設(shè)置私有成員變量mShiftingMode可以修改
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                //去除shift效果
                item.setShiftingMode(false);
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("BNVHelper", "沒有mShiftingMode這個成員變量", e);
        } catch (IllegalAccessException e) {
            Log.e("BNVHelper", "無法修改mShiftingMode的值", e);
        }
    }

需要注意的是按鈕圖標貌似不能使用png番刊,只能使用xml含鳞。
最后我們可能還需要添加未讀消息的角標

BottomNavigationMenuView menuView = (BottomNavigationMenuView) navigation.getChildAt(0);
        //這里就是獲取所添加的每一個Tab(或者叫menu),
        View tab = menuView.getChildAt(4);
        BottomNavigationItemView itemView = (BottomNavigationItemView) tab;
        //加載我們的角標View芹务,新創(chuàng)建的一個布局
        View badge = LayoutInflater.from(this).inflate(R.layout.message_number, menuView, false);
        //添加到Tab上
        itemView.addView(badge);

        TextView count = badge.findViewById(R.id.tv_msg_count);
        count.setText(String.valueOf(10));

        //如果沒有消息蝉绷,不需要顯示的時候那只需要將它隱藏即可
//        count.setVisibility(View.GONE);

message_number.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="android.my.com.myandroid.MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_msg_count"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_gravity="center"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_marginTop="3dp"
        android:background="#f00"
        android:gravity="center"
        android:textColor="#fff"
        android:textSize="6dp"/>

</RelativeLayout>

下載地址:https://github.com/lllllliudahong/BottomNavigationView

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市枣抱,隨后出現(xiàn)的幾起案子熔吗,更是在濱河造成了極大的恐慌,老刑警劉巖佳晶,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件磁滚,死亡現(xiàn)場離奇詭異,居然都是意外死亡宵晚,警方通過查閱死者的電腦和手機垂攘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來淤刃,“玉大人晒他,你說我怎么就攤上這事∫菁郑” “怎么了陨仅?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵津滞,是天一觀的道長。 經(jīng)常有香客問我灼伤,道長触徐,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任狐赡,我火速辦了婚禮撞鹉,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘颖侄。我一直安慰自己鸟雏,他們只是感情好,可當我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布览祖。 她就那樣靜靜地躺著孝鹊,像睡著了一般。 火紅的嫁衣襯著肌膚如雪展蒂。 梳的紋絲不亂的頭發(fā)上又活,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天,我揣著相機與錄音锰悼,去河邊找鬼皇钞。 笑死,一個胖子當著我的面吹牛松捉,可吹牛的內(nèi)容都是我干的夹界。 我是一名探鬼主播,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼隘世,長吁一口氣:“原來是場噩夢啊……” “哼可柿!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起丙者,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤复斥,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后械媒,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體目锭,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年纷捞,在試婚紗的時候發(fā)現(xiàn)自己被綠了痢虹。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡主儡,死狀恐怖奖唯,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情糜值,我是刑警寧澤丰捷,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布坯墨,位于F島的核電站,受9級特大地震影響病往,放射性物質(zhì)發(fā)生泄漏捣染。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一停巷、第九天 我趴在偏房一處隱蔽的房頂上張望耍攘。 院中可真熱鬧,春花似錦叠穆、人聲如沸少漆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至渗磅,卻和暖如春嚷硫,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背始鱼。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工仔掸, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人医清。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓起暮,卻偏偏與公主長得像,于是被迫代替她去往敵國和親会烙。 傳聞我的和親對象是個殘疾皇子负懦,可洞房花燭夜當晚...
    茶點故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容