Android BottomNavigationView底部导航效果
作者:张小贝_ 时间:2023-09-20 20:11:57
BottomNavigationView 很早之前就在 Material Design 中出现了,但是直到 Android Support Library 25 中才增加了 BottomNavigationView 控件。也就是说如果使用官方的BottomNavigationView控件必须让targetSdkVersion >= 25,这样才能引入25版本以上的兼容包。
接下来我们来看看如何使用BottomNavigationView。
使用BottomNavigationView 需要添加design兼容包的依赖。
dependencies {
//...
compile 'com.android.support:design:25.1.0'
}
在 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/call"
android:icon="@android:drawable/ic_menu_call"
android:title="call" />
<item
android:id="@+id/message"
android:icon="@android:drawable/ic_dialog_email"
android:title="message" />
<item
android:id="@+id/search"
android:icon="@android:drawable/ic_menu_search"
android:title="搜索" />
<item
android:id="@+id/delete"
android:icon="@android:drawable/ic_menu_delete"
android:title="删除"/>
</menu>
每个item表示底部导航的一个条目,icon是图标,title是文字。
然后修改Activity布局
<?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:id="@+id/activity_bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="演示内容"
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>
BottomNavigationView有几个特殊的属性,
* itemtBackground 条目背景
* itemIcoTint 图标渲染的颜色
* itemtTextColor 文字的颜色
* menu 关联上面创建的菜单
最后修改BottomNavigationViewActivity代码
public class BottomNavigationViewActivity extends AppCompatActivity {
private TextView textView;
private BottomNavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_navigation_view);
textView = (TextView) findViewById(R.id.text);
navigationView = (BottomNavigationView) findViewById(R.id.navigation);
//选中条目的监听事件
navigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
textView.setText(item.getTitle().toString());
return true;
}
});
}
}
注意事项
* 底部导航栏默认高度是56dp
* 菜单建议是3-5个
标签:Android,BottomNavigationView,底部导航
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java Runtime的使用详解
2021-10-23 06:46:39
![](https://img.aspxhome.com/file/2023/6/78976_0s.png)
Java设计模式之单例模式简介
2023-12-11 12:04:08
用C语言实现五子棋小游戏
2023-01-19 12:56:58
![](https://img.aspxhome.com/file/2023/9/117779_0s.jpg)
java 类加载与自定义类加载器详解
2022-05-21 04:31:31
![](https://img.aspxhome.com/file/2023/4/92694_0s.png)
C#把数字转换成大写金额的代码实例
2022-03-21 08:08:48
一文详解Spring的Enablexxx注解使用实例
2023-09-03 08:43:18
![](https://img.aspxhome.com/file/2023/6/122036_0s.png)
Android Spinner 组件的应用实例
2022-07-12 04:50:13
Android 给控件添加边框阴影效果
2021-08-13 17:12:07
c#使用反射调用类型成员示例
2023-02-27 08:54:43
Spring学习笔记之bean的基础知识
2021-09-08 10:09:27
![](https://img.aspxhome.com/file/2023/3/63433_0s.png)
Java设计模式之备忘录模式实现对象状态的保存和恢复
2023-08-28 15:22:27
![](https://img.aspxhome.com/file/2023/9/58439_0s.png)
c#使用Socket发送HTTP/HTTPS请求的实现代码
2023-10-12 07:10:00
![](https://img.aspxhome.com/file/2023/6/80046_0s.jpg)
C++实现特殊矩阵的压缩存储算法
2023-11-17 23:53:06
![](https://img.aspxhome.com/file/2023/3/120793_0s.png)
C#多维数组学习使用
2023-07-06 11:51:02
Hibernate批量处理海量数据的方法
2023-07-30 08:12:04
理解java和python类变量以及类的成员变量
2023-09-14 19:42:55
Android使用glide加载gif动画设置播放次数
2022-06-20 03:59:46
Android App中使用ViewPager+Fragment实现滑动切换效果
2023-01-12 19:51:50
![](https://img.aspxhome.com/file/2023/3/137203_0s.jpg)
Java使用新浪微博API通过账号密码方式登陆微博的实例
2023-09-23 05:35:38
Unity3d 使用Gizmos画一个圆圈
2022-05-22 05:10:52
![](https://img.aspxhome.com/file/2023/2/104332_0s.jpg)