android.support.design.widget.BottomNavigationView+fragment

效果图


Screenshot_20180102-154959.png

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

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

具体添加什么版本的依赖包,你可以手动点击new->Activity->Bottom Navigation Activity系统会自动导入
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" />
    <!--底部导航栏高度默认是 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去监听按钮点击切换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都置为隐藏状态。
     *
     * @param transaction 用于对Fragment执行操作的事务
     */
    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 {
            //设置私有成员变量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,新创建的一个布局
        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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容