[译]三分钟玩转BottomNavigationView

原文 BottomNavigationView By Mark Allison

Google 在2016年发不了Nougat 7.1开发者预览的release版,同时还有Android Design Support Library 25。一个被名为BottomNavigationView 的新控件提供更简洁的方式实现bottom navigation bar模式,正好是符合材料设计规范的。在这篇文章中,让我们一探究竟。

在看代码之前,我们不会深入探讨为什么Android要使用底部导航栏,美化Android更在于“如何做”和“是否应该”,所以让别人讨论为什么吧。如果你觉得BottomNavigationView符合你的应用,这篇文章正好给出基础用法,真的比较简单。

值得指出Javadoc中关于BottomNavigationView包括了如何使用的代码片段,不幸的是(在我写的时候),demo代码由于包名和命名空间的问题并不能跑起来。

我并不喜欢demo代码毫无道理地使用了ConstraintLayout,不论它是不是beta版。BottomNavigationView 并不需要它,demo代码应该能使用任何的布局。

好,我们先声明一个布局文件包含了BottomNavigationView

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.stylingandroid.bottomnavigationview.MainActivity">
 
  <FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/navigation_view"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>
 
  <android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation"/>

</android.support.constraint.ConstraintLayout>

唯一不标准的是app:menu属性,官方文档的问题在于指定了一个固定的包名,而我们使用时只要绑定http://schemas.android.com/apk/res-auto 就行了。

menu属性置顶标准的menu资源文件,例如

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
 
  <item
    android:id="@+id/recents"
    android:icon="@drawable/ic_recents"
    android:title="@string/recents" />
 
  <item
    android:id="@+id/favourites"
    android:icon="@drawable/ic_favourites"
    android:title="@string/favourites" />
 
  <item
    android:id="@+id/nearby"
    android:icon="@drawable/ic_nearby"
    android:title="@string/nearby" />
</menu>

如果你正在迁移一个基于menu资源文件的导航栏,那真是轻松愉快。
既然我们需要使用标准的menu资源文件,那么就要符合标准逻辑。然而BottomNavigationView需要添加更多的属性例如选中状态。我们通过继承BottomNavigationView.OnNavigationItemSelectedListener来实现。在这个例子中,我们只改变Fragment中的文字(保持代码简单整洁),也许实际中做更有趣的事。

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
 
    private TextFragment fragment;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragment = new TextFragment();
 
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_container, fragment);
        transaction.commit();
 
        BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation_view);
 
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
    }
 
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        @StringRes int text;
        switch (item.getItemId()) {
            case R.id.recents:
                text = R.string.recents;
                break;
            case R.id.favourites:
                text = R.string.favourites;
                break;
            case R.id.nearby:
                text = R.string.nearby;
                break;
            default:
                return false;
        }
        switchFragmentText(text);
        return true;
    }
 
    private void switchFragmentText(@StringRes int text) {
        fragment.setText(text);
    }
}

最后的效果图。

《[译]三分钟玩转BottomNavigationView》 效果图

有意思的事(通常是Design Support Library的特性)是主题会 根据我们在theme定义的颜色来走。不过,如果需要自定义,这里也会属性可以设置。

想在XML里个性化主题,背景,图标,文字颜色,官方文档 说得比我好。

这就是全部内容。不管喜不喜欢,底部导航栏都已经是材料设计的一部分了,有Design Support Library的支持实现起来太简单了。

    原文作者:豆沙包67
    原文地址: https://www.jianshu.com/p/0bf69a9e52d0
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞