一直感觉Material Design设计很是好看,尤其是底部的导航栏,简约的图标以及透明的背景。决定自己体验一把。
Material Design是由Google推出的全新的设计语言,谷歌表示,这种设计语言旨在为手机、平板电脑、台式机和“其他平台”提供更一致、更广泛的“外观和感觉”。
Material Design的一些重要功能包括系统字体Roboto的升级版本,同时颜色更鲜艳,动画效果更突出。谷歌的想法是让谷歌平台上的开发者掌握这个新框架,从而让所有应用就有统一的外观,就像是苹果向开发者提出的设计原则一样。
Bottom Navigation经常用于底部导航栏。不过它所包含的标签页不应过多也不应该过少,谷歌官方文档中表示,标签页的个数在3-5个左右合适。
在Module的build.gradle中添加如下代码
compile 'com.roughike:bottom-bar:2.0.2'
实现Bottom Navigation功能,比较好用的是第三方的BottomBar库,截止2017.1.8,最新的版本是2.0.2,如果需要最新版,请访问 BottomBar的Github Repository
在res下新建类型为xml的xml文件夹
在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
属性,则是控制着激活时的图片颜色。
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的属性。
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);
}
}
修改主题样式
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>
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" />
the XML Resource id for your tabs, that reside in values/xml/
if you want the BottomBar to behave differently for tablets. There’s an example of this in the sample project!
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!the alpha value for inactive tabs, that’s used in the tab icons and titles.
the alpha value for active tabs, that’s used in the tab icons and titles.
the color for inactive tabs, that’s used in the tab icons and titles.
the color for active tabs, that’s used in the tab icons and titles.
the background color for any Badges in this BottomBar.
custom textAppearance for the titles
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.
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" />
the color for inactive tabs, that’s used in the tab icons and titles.
the color for active tabs, that’s used in the tab icons and titles.
the color that the whole BottomBar should be when selected this tab.
the background color for any Badges in this tab.
<com.roughike.bottombar.BottomBar>
标签中添加属性android:layout_alignParentBottom="true"
,并且做好第六步工作。