MD筆記——Bottom Navigation

效果圖

2017-01-03 20.42.27.gif

組件介紹

Bottom Navigation經(jīng)常用于底部導(dǎo)航欄施掏。不過它所包含的標(biāo)簽頁不應(yīng)過多也不應(yīng)該過少滑黔,谷歌官方文檔中表示趁蕊,標(biāo)簽頁的個(gè)數(shù)在3-5個(gè)左右合適目派。

如何實(shí)現(xiàn)

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

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

實(shí)現(xiàn)Bottom Navigation功能英遭,比較好用的是第三方的BottomBar庫间护,截止2017.1.8,最新的版本是2.0.2挖诸,如果需要最新版兑牡,請?jiān)L問 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="財(cái)務(wù)"
            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屬性控制著當(dāng)這個(gè)Tab被選擇時(shí)均函,BottomBar的整體顏色。inActiveColor屬性是這個(gè)Tab未激活時(shí)圖片的顏色菱涤。與之相對應(yīng)的activeColor屬性苞也,則是控制著激活時(shí)的圖片顏色。

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文件如迟,里面有每個(gè)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);
        //設(shè)置Bottom Bar的選擇監(jiān)聽器
        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {
                if (tabId == R.id.tab_one){
                    tv.setText("tab_one 被點(diǎn)擊");
                }
                if (tabId == R.id.tab_two){
                    tv.setText("tab_group 被點(diǎn)擊");
                }
                if (tabId == R.id.tab_three){
                    tv.setText("tab_three 被點(diǎn)擊");
                }
            }
        });
        //設(shè)置Bottom Bar的重復(fù)選擇監(jiān)聽器
        bottomBar.setOnTabReselectListener(new OnTabReselectListener() {
            @Override
            public void onTabReSelected(@IdRes int tabId) {
                if (tabId == R.id.tab_one){
                    tv.setText("tab_one 再次被點(diǎn)擊");
                }
                if (tabId == R.id.tab_two){
                    tv.setText("tab_two 再次被點(diǎn)擊");
                }
                if (tabId == R.id.tab_three){
                    tv.setText("tab_three 再次被點(diǎn)擊");
                }
            }
        });
        //設(shè)置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的圖標(biāo)必須是全透明此再、純黑24dp玲销、Padding = 0dp输拇,而且最好所有分辨密度的圖片都要有,否則圖標(biāo)會(huì)很大或者很小贤斜,影響顯示效果策吠。
  • 如果想保持在屏幕最底部,記得在布局xml的<com.roughike.bottombar.BottomBar>標(biāo)簽中添加屬性 android:layout_alignParentBottom="true"瘩绒,并且做好第六步工作猴抹。

參考文檔

與FloatingActionButton和SnackBar合作的效果

2017-01-20 21.41.52.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市锁荔,隨后出現(xiàn)的幾起案子蟀给,更是在濱河造成了極大的恐慌,老刑警劉巖阳堕,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件跋理,死亡現(xiàn)場離奇詭異,居然都是意外死亡嘱丢,警方通過查閱死者的電腦和手機(jī)薪介,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進(jìn)店門祠饺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來越驻,“玉大人,你說我怎么就攤上這事道偷∽号裕” “怎么了?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵勺鸦,是天一觀的道長并巍。 經(jīng)常有香客問我,道長换途,這世上最難降的妖魔是什么懊渡? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮军拟,結(jié)果婚禮上剃执,老公的妹妹穿的比我還像新娘。我一直安慰自己懈息,他們只是感情好肾档,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般怒见。 火紅的嫁衣襯著肌膚如雪俗慈。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天遣耍,我揣著相機(jī)與錄音闺阱,去河邊找鬼。 笑死配阵,一個(gè)胖子當(dāng)著我的面吹牛馏颂,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播棋傍,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼救拉,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了瘫拣?” 一聲冷哼從身側(cè)響起亿絮,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎麸拄,沒想到半個(gè)月后派昧,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡拢切,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年蒂萎,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片淮椰。...
    茶點(diǎn)故事閱讀 40,021評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡五慈,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出主穗,到底是詐尸還是另有隱情泻拦,我是刑警寧澤,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布忽媒,位于F島的核電站争拐,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏晦雨。R本人自食惡果不足惜架曹,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望闹瞧。 院中可真熱鬧绑雄,春花似錦、人聲如沸夹抗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至杏愤,卻和暖如春靡砌,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背珊楼。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工通殃, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留报慕,地道東北人玛臂。 一個(gè)月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像介褥,于是被迫代替她去往敵國和親已慢。 傳聞我的和親對象是個(gè)殘疾皇子曲聂,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,974評論 2 355

推薦閱讀更多精彩內(nèi)容

  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,434評論 2 45
  • UDID 全名是 Unique Device Identifier 佑惠,設(shè)備唯一標(biāo)識符朋腋。適合設(shè)備有關(guān)的,而且只和設(shè)備...
    nemoispretty閱讀 388評論 0 0
  • 今天星期二膜楷,第一節(jié)課就是數(shù)學(xué)課旭咽。 我和同學(xué)們用數(shù)學(xué)學(xué)具來拼搭積木房子。數(shù)學(xué)老師還讓我們用小棒來擺數(shù)字赌厅,而且讓我們用...
    吳銳韜閱讀 337評論 0 1
  • 早在七八年前穷绵,想去甘肅,尤其是想去敦煌特愿,就在我的夢想list里存在很久了仲墨。幻想中的大西北洽议,是一望無際的戈壁灘宗收;藍(lán)天...
    蛋撻少女啊閱讀 282評論 0 0