MD笔记——Bottom Navigation

效果图

2017-01-03 20.42.27.gif

组件介绍

Bottom Navigation经常用于底部导航栏。不过它所包含的标签页不应过多也不应该过少,谷歌官方文档中表示,标签页的个数在3-5个左右合适。

如何实现

1、在Module的build.gradle中添加如下代码

compile 'com.roughike:bottom-bar:2.0.2'

实现Bottom Navigation功能,比较好用的是第三方的BottomBar库,截止2017.1.8,最新的版本是2.0.2,如果需要最新版,请访问 BottomBar 的 Github Repository

2、在res下新建类型为xml的xml文件夹

QQ20170103-0@2x.png

QQ20170103-1@2x.png

3、在xml文件夹下新建bottombar_tabs.xml文件

res/xml/bottombar_tabs.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <tabs>
        <tab
            id="@+id/tab_one"
            icon="@drawable/ic_3d_rotation_white_24dp"
            title="财务"
            barColorWhenSelected="#865242"
            inActiveColor="#FFFFFF"
            activeColor = "#FFFFFF"/>
        <tab
            id="@+id/tab_two"
            icon="@drawable/ic_account_balance_white_24dp"
            title="群组"
            barColorWhenSelected="#268434"
            inActiveColor="#FFFFFF"
            activeColor = "#FFFFFF"/>
        <tab
            id="@+id/tab_three"
            icon="@drawable/ic_accessibility_white_24dp"
            title="朋友"
            barColorWhenSelected="#8b2099"
            inActiveColor="#FFFFFF"
            activeColor = "#FFFFFF"/>
    </tabs>
</PreferenceScreen>

barColorWhenSelected属性控制着当这个Tab被选择时,BottomBar的整体颜色。inActiveColor属性是这个Tab未激活时图片的颜色。与之相对应的activeColor属性,则是控制着激活时的图片颜色。

4、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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="me.luzhoumin.bottomnavigation.MainActivity">

    <com.roughike.bottombar.BottomBar
        android:id="@+id/bottomBar"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        app:bb_tabXmlResource="@xml/bottombar_tabs"
        app:bb_behavior="underNavbar"
        app:bb_inActiveTabAlpha="0.6"
        app:bb_activeTabAlpha="1"
        app:bb_showShadow="true"/>

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="36sp"
        android:text="Hello World!"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="51dp" />

</RelativeLayout>

app:bb_tabXmlResource指向的是第三步建的xml文件,里面有每个Tab的属性。

5、MainActivity.java

import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.BottomBarTab;
import com.roughike.bottombar.OnTabReselectListener;
import com.roughike.bottombar.OnTabSelectListener;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取Bottom Bar组件
        BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);
        //获取用来显示的TextView组件
        final TextView tv = (TextView) findViewById(R.id.textview);
        //设置Bottom Bar的选择监听器
        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {
                if (tabId == R.id.tab_one){
                    tv.setText("tab_one 被点击");
                }
                if (tabId == R.id.tab_two){
                    tv.setText("tab_group 被点击");
                }
                if (tabId == R.id.tab_three){
                    tv.setText("tab_three 被点击");
                }
            }
        });
        //设置Bottom Bar的重复选择监听器
        bottomBar.setOnTabReselectListener(new OnTabReselectListener() {
            @Override
            public void onTabReSelected(@IdRes int tabId) {
                if (tabId == R.id.tab_one){
                    tv.setText("tab_one 再次被点击");
                }
                if (tabId == R.id.tab_two){
                    tv.setText("tab_two 再次被点击");
                }
                if (tabId == R.id.tab_three){
                    tv.setText("tab_three 再次被点击");
                }
            }
        });
        //设置Tab的Badge
        BottomBarTab nearby = bottomBar.getTabWithId(R.id.tab_two);
        nearby.setBadgeCount(5);
    }
}

6、修改主题样式

res/values/styles.xml

<resources>
    <!-- Base application theme. -->
    <!--修改主题为无ActionBar-->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <!--添加-->
        <item name="android:navigationBarColor">@android:color/transparent</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    </style>
</resources>

BottomBar API

For the BottomBar

<com.roughike.bottombar.BottomBar
    android:id="@+id/bottomBar"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    app:bb_tabXmlResource="@xml/bottombar_tabs_three"
    app:bb_tabletMode="true"
    app:bb_behavior="shifting|shy|underNavbar"
    app:bb_inActiveTabAlpha="0.6"
    app:bb_activeTabAlpha="1"
    app:bb_inActiveTabColor="#222222"
    app:bb_activeTabColor="@color/colorPrimary"
    app:bb_titleTextAppearance="@style/MyTextAppearance"
    app:bb_titleTypeFace="fonts/MySuperDuperFont.ttf"
    app:bb_showShadow="true" />

bb_tabXmlResource
the XML Resource id for your tabs, that reside in values/xml/
bb_tabletMode
if you want the BottomBar to behave differently for tablets. There's an example of this in the sample project!
bb_behavior
shifting: the selected tab is wider than the rest.
shy: put the BottomBar inside a CoordinatorLayout and it'll automatically hide on scroll!
underNavbar: draw the BottomBar under the navBar!
bb_inActiveTabAlpha
the alpha value for inactive tabs, that's used in the tab icons and titles.
bb_activeTabAlpha
the alpha value for active tabs, that's used in the tab icons and titles.
bb_inActiveTabColor
the color for inactive tabs, that's used in the tab icons and titles.
bb_activeTabColor
the color for active tabs, that's used in the tab icons and titles.
bb_badgeBackgroundColor
the background color for any Badges in this BottomBar.
bb_titleTextAppearance
custom textAppearance for the titles
bb_titleTypeFace
path for your custom font file, such as fonts/MySuperDuperFont.ttf. In that case your font path would look like src/main/assets/fonts/MySuperDuperFont.ttf, but you only need to provide fonts/MySuperDuperFont.ttf, as the asset folder will be auto-filled for you.
bb_showShadow
controls whether the shadow is shown or hidden, defaults to true.

For the tabs

<tab
    id="@+id/tab_recents"
    title="Recents"
    icon="@drawable/empty_icon"
    inActiveColor="#00FF00"
    activeColor="#FF0000"
    barColorWhenSelected="#FF0000"
    badgeBackgroundColor="#FF0000" />

inActiveColor
the color for inactive tabs, that's used in the tab icons and titles.
activeColor
the color for active tabs, that's used in the tab icons and titles.
barColorWhenSelected
the color that the whole BottomBar should be when selected this tab.
badgeBackgroundColor
the background color for any Badges in this tab.

需要注意的地方

  • Tab的图标必须是全透明纯黑24dpPadding = 0dp,而且最好所有分辨密度的图片都要有,否则图标会很大或者很小,影响显示效果。
  • 如果想保持在屏幕最底部,记得在布局xml的<com.roughike.bottombar.BottomBar>标签中添加属性 android:layout_alignParentBottom="true",并且做好第六步工作。

参考文档

与FloatingActionButton和SnackBar合作的效果

2017-01-20 21.41.52.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,569评论 2 45
  • UDID 全名是 Unique Device Identifier ,设备唯一标识符。适合设备有关的,而且只和设备...
    nemoispretty阅读 401评论 0 0
  • 今天星期二,第一节课就是数学课。 我和同学们用数学学具来拼搭积木房子。数学老师还让我们用小棒来摆数字,而且让我们用...
    吴锐韬阅读 379评论 0 1
  • 早在七八年前,想去甘肃,尤其是想去敦煌,就在我的梦想list里存在很久了。幻想中的大西北,是一望无际的戈壁滩;蓝天...
    蛋挞少女啊阅读 294评论 0 0