Android底部导航菜单介绍,Android底部导航栏之官方BottomNavigationView

今天将AS升级2.3.1之后新建工程,忽然发现竟然多了这个!!!看来不得不去了解了,毕竟是官方控件

f4033b93d43c

Bottom Navigation Activity

一. 简介

Represents a standard bottom navigation bar for application. It is an implementation of material design bottom navigation.

应用底部的导航栏,是material design bottom navigation的实现.

特性,使用场景

Bottom navigation bars make it easy for users to explore and switch between top-level views in a single tap. It should be used when application has three to five top-level destinations.

底部导航使用户更方便的查看和切换最高层级的导航界面,适用于有三到五个Tab的情况(推荐是三到五个,太多和太少都不应该用)

二. 用法

build.gradle加入支持库

compile 'com.android.support:design:25.2.0'

XML使用布局(只是NavigationView,具体Tab要在menu里添加)

android:id="@+id/navigation"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="bottom"

android:background="?android:attr/windowBackground"

app:menu="@menu/navigation"/>

添加Tab

3.1 官方介绍

The bar contents can be populated by specifying a menu resource file. Each menu item title, icon and enabled state will be used for displaying bottom navigation bar items. Menu items can also be used for programmatically selecting which destination is currently active. It can be done using MenuItem#setChecked(true)

可以通过menu的方式向BottomNavigationView添加Tab,每个menu可以有title,icon,enable状态,可以通过menu的checked属性设置默认选中的Tab(我自己设置无效)

3.2 添加Menu

android:id="@+id/navigation_home"

android:icon="@drawable/ic_home_black_24dp"

android:title="@string/title_home"/>

android:id="@+id/navigation_dashboard"

android:icon="@drawable/ic_dashboard_black_24dp"

android:title="@string/title_dashboard"/>

android:id="@+id/navigation_notifications"

android:icon="@drawable/ic_notifications_black_24dp"

android:title="@string/title_notifications"

android:enabled="false"/>

3.3 设置点击事件

BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);

navigation.getMenu().getItem(2).setChecked(true);//设置默认选中的Tab

navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

@Override

public boolean onNavigationItemSelected(@NonNull MenuItem item) {

/*通过判断进行选择Tab之后想要的操作,一般是进行fragment的动态切换*/

switch (item.getItemId()) {

case R.id.navigation_home:

mTextMessage.setText(R.string.title_home);

return true;

case R.id.navigation_dashboard:

mTextMessage.setText(R.string.title_dashboard);

return true;

case R.id.navigation_notifications:

mTextMessage.setText(R.string.title_notifications);

return true;

}

return false;

}

});

三. XML属性介绍

Caution:以下三种属性都可以用ColorStateList来实现点击前后颜色变化效果

itemBackground 设置NavigationView的背景颜色,效果如下图:

f4033b93d43c

itemBackground效果图

itemIconTint 设置ItemIcon的颜色,效果如下图:

f4033b93d43c

itemIconTint效果

itemTextColor设置itemText的颜色,效果如下:

f4033b93d43c

itemTextColor效果

PS:以上三个属性不设置的话默认背景颜色为background,图标和字体颜色为灰色

四. Tab点击后的颜色设置

新建ColorStateList(colot_item_state.xml)

android:color="@color/colorAccent"/>

android:color="@color/colorPrimary"/>

该文件要在res文件夹下新建color目录,然后新建该xml文件

通过NavigationView的三个XML属性设置该ColorStateList

android:id="@+id/navigation"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="bottom"

android:background="?android:attr/windowBackground"

app:menu="@menu/navigation"

app:itemIconTint="@color/colot_item_state"

app:itemTextColor="@color/colot_item_state"/>

效果:

f4033b93d43c

点击效果

五. 不同Tab数量的效果图

三个Tab:

f4033b93d43c

三个Tab的点击效果

四个及以上Tab

f4033b93d43c

四个Tab

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值