转载请注明出处:http://blog.csdn.net/wl9739/article/details/52875710

BottomNavigationView 很早之前就在 Material Design 中出现了,但是直到 Android Support Library 25 中才增加了 BottomNavigationView 控件。

该控件使用方法如下:

  • 在 build.gradle 文件中增加依赖:
compile 'com.android.support:design:25.0.0'
  • 1
  • 1
  • 在 res/menu/ 目录下创建一个 xml 文件(没有该目录则手动创建一个),我将其命名为 navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/add"
android:icon="@drawable/ic_call_white_24dp"
android:title="call" />
<item
android:id="@+id/delete"
android:icon="@drawable/ic_announcement_white_24dp"
android:title="message" />
<item
android:id="@+id/setting"
android:icon="@drawable/ic_settings_white_24dp"
android:title="setting" /> <item
android:id="@+id/me"
android:icon="@drawable/ic_account_circle_white_24dp"
android:title="me"/>
</menu>

在布局文件中添加如下代码即可:

<android.support.design.widget.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/navigation"/>

注意这里的 app:menu="@menu/navigation" 引用了刚才创建的菜单文件。

最后,在代码中添加 BottomNavigationView 的事件监听:

navigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
......
return true;
}
});

这样就完成了一个简单的 BottomNavigationView 控件。该控件有几个地方需要注意的:

  • 底部导航栏高度默认是 56dp。

  • 菜单元素只能是 3~5 个。如果个数少于3个或者多于5个,则会报错。
  • icon 的选中颜色默认是 @color/colorPrimary。当然你也可以使用 app:itemIconTint="@android:color/white" 来自定义,这样定以后,所有的 icon 颜色都是这个了。
  • 菜单元素文字的默认颜色是 @color/colorPrimary。你可以使用 app:itemTextColor="@android:color/white" 自定义。
  • 底部导航栏背景颜色默认是当前样式的背景色(白色/黑色),你可以使用 app:itemBackground="@android:color/black" 来更改。

写个具体的例子吧。比如新建一个项目,activity_main.xml 布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="me.qiushui.buttomnavigationdemo.MainActivity"> <TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello World!"
android:textSize="36sp"/> <android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@android:color/black"
app:itemIconTint="@android:color/white"
app:itemTextColor="@android:color/white"
app:menu="@menu/navigation"/>
</RelativeLayout>

菜单文件还是沿用开始创建的那个文件(图片资源自己添加)。MainActivity.Java 代码如下:

public class MainActivity extends AppCompatActivity {

    TextView mTextView;
private BottomNavigationView mNavigationView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.text);
mNavigationView = (BottomNavigationView) findViewById(R.id.navigation); mNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
mTextView.setText(item.getTitle().toString().toUpperCase());
return true;
}
});
}
}

运行效果如下图:

BottomNavigationView 的使用的更多相关文章

  1. 关于Android中使用BottomNavigationView切换横屏导致返回主页的问题

    问题: 如图,"发现"页即为主页,然后我们切换到"我"页,一切正常. 那么问题来了,如果切换到"我"页后把手机横屏,则会出现下面的情况. 嗯 ...

  2. 第三十七篇-BottomNavigationVIew底部导航的使用

    效果图: 添加底部导航和viewpaper 设置底部导航在底部 app:layout_constraintBottom_toBottomOf="parent" 新建四个fragme ...

  3. BottomNavigationView 使用

    <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.Cons ...

  4. Android 底部按钮BottomNavigationView + Fragment 的使用(二)

    这里来试验BottomNavigationView + Fragment 底部按钮通过点击底部选项,实现中间的Fragment进行页面的切换. 使用BottomNavigationView 控件,实现 ...

  5. Android 底部按钮BottomNavigationView + Fragment + viewPager 的使用(一)

    实现的效果,左右滑动,底部栏跟着滑动,中间加的是分帧的页面        上代码:主页面activity_main.xml <?xml version="1.0" encod ...

  6. BottomNavigationView结合ViewPager

    BottomNavigationView是Google推出的底部导航栏组件,在没有这些底部导航组件之前,Android开发者多使用的是RadioGroup,在上一个项目开发中我们使用了Google的B ...

  7. BottomNavigationView的使用

    BottomNavigationView的使用 废话少说, 先看东西 依赖 implementation 'com.android.support:design:26.1.0' 布局 //用这个控件需 ...

  8. 014 Android BottomNavigationView 底部导航组件使用

    1.导入BottomNavigationView组件(点击下载按钮,安装组件) 2.新建菜单 (1)app--->src-->main--->res ,选中res目录右击new--- ...

  9. android BottomNavigationView 底部显示3个以上的item

    你现在可以用app:labelVisibilityMode="[labeled, unlabeled, selected, auto] labeled 所有的标签都是可见的. unlabel ...

随机推荐

  1. 剑指Offer-29.最小的K个数(C++/Java)

    题目: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 分析: 最先想到的是将数组升序排列,返回前k个元素.不过排序的话效率 ...

  2. JavaScript笔记四

    1.运算符 逻辑运算符 ! - 非运算可以对一个布尔值进行取反,true变false false边true - 当对非布尔值使用!时,会先将其转换为布尔值然后再取反 - 我们可以利用!来将其他的数据类 ...

  3. python变量、输入输出-xdd

    1.注释 #输入身高,计算BMI 注释1,单行注释... 注释2,多行注释xiedong.. 2.中文编码声明,UTF-8编码声明 # coding=编码 # coding=utf-8 3.建议每行不 ...

  4. BIOS和CMOS概念整理

    一:什么是BIOS  BIOS(Basic Input Output System),基本输入输出系统.是被写死在主板ROM只读芯片中的一组程序,在开机的时候首先要去读取的一个小程序. 它是我们可以将 ...

  5. webuploader 快速应用(C#)

    百度的WebUploader前端插件作为目前比较好用且免费的附件上传工具,利用了断点续传特点实现了大文件上传功能,其更好的兼容性与界面效果完全可以替换掉IE的activex 上传控件.许多人或许还不知 ...

  6. Glibc编译报错:*** LD_LIBRARY_PATH shouldn't contain the current directory when*** building glibc. Please change the environment variable

    执行glibc编译出错如下图 [root@localhost tmpdir]# ../configure --prefix=/usr/loacl/glibc2.9 --disable-profile ...

  7. django ListView

    context_object_name = 'posts'. The template default name is ListView 'object_list' from .models impo ...

  8. Gzip,BZip2,Lzo,Snappy总结

    gzip,bzip2,lzo,snappy是hadoop中比较常见的文件压缩格式,可以节省很多硬盘存储,以下是Gzip , BZip2 , Lzo Snappy 四种方式的优缺点 和使用场景 Gzip ...

  9. LESSON 3- Discrete Memory-less Sources

    1.     Entropy H[X] - bounds on Lmin 2.      Huffman’s algorithm for optimal source code

  10. CCNA 之 十 ACL 访问控制列表

    ACL 访问控制列表 ACL(Access Control List) 接入控制列表 ACL 的量大主要功能: 流量控制 匹配感兴趣流量 标准访问控制列表 只能根据源地址做过滤 针对曾哥协议采取相关动 ...