安卓开发入门之底部导航BottomNavigationView(翻译)

来源:互联网 发布:java创建map对象 编辑:程序博客网 时间:2024/06/05 04:40
原文链接
http://www.coderzheaven.com/2017/06/15/bottom-navigation-demo-in-android/
1.在app的module添加依赖
compile 'com.android.support:appcompat-v7:25.1.0'    compile 'com.android.support:design:25.1.0'    compile 'com.android.support:support-v4:25.1.0'
2.在布局文件中添加控件
<android.support.design.widget.BottomNavigationView       android:id="@+id/navigation"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:layout_gravity="start"       design:menu="@menu/bottom_nav_items" />
3.在res/menu文件夹中添加文件bottom_nav_items.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:id="@+id/menu_home"        android:title="@string/menu_home"        android:icon="@drawable/ic_home_black"        />    <item        android:id="@+id/menu_search"        android:title="@string/menu_search"        android:icon="@drawable/ic_search_black"        />     <item        android:id="@+id/menu_notifications"        android:title="@string/menu_notifications"        android:icon="@drawable/ic_notifications_black" /></menu>
4.在Activity或Fragment中监听
mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {            @Override            public boolean onNavigationItemSelected(@NonNull MenuItem item) {                // handle desired action here                // One possibility of action is to replace the contents above the nav bar                // return true if you want the item to be displayed as the selected item                return true;            }        });

运行结果;


源码下载地址
https://github.com/MrVipinVijayan/BottomNavigationDemo



原创粉丝点击