Android学习之BottomNavigationBar实现Android特色底部导航栏

MarianaEsb 6年前
   <p>Android底部导航栏的实现方式特别多,例如TabHost,TabLayout,或者TextView等,都可以实现底部导航栏的效果,但是却没有Google官方统一的导航栏样式,今天讲的就是Google最近添加到Material design中的底部导航栏BottomNavigationBar,也可以说是现今Android底部导航栏的一个标准与统一吧。</p>    <p>效果:</p>    <p><img alt="这里写图片描述" src="https://simg.open-open.com/show/b3fa17996c1e8aa6f78b4b91729de9e9.gif"></p>    <p>实现效果:</p>    <p><img alt="这里写图片描述" src="https://simg.open-open.com/show/7d7df7da3d5a93863a7a5cc031145421.png"></p>    <p>实现:</p>    <p>1.下载jar包<br> 2.添加Maven</p>    <pre>  <code class="language-java"><dependency>    <groupId>com.ashokvarma.android</groupId>    <artifactId>bottom-navigation-bar</artifactId>    <version>1.2.0</version>    <type>pom</type>  </dependency></code></pre>    <p>3.添加依赖。</p>    <pre>  <code class="language-java">compile 'com.ashokvarma.android:bottom-navigation-bar:1.2.0'</code></pre>    <p>4.添加Ivy</p>    <pre>  <code class="language-java"><dependency org='com.ashokvarma.android' name='bottom-navigation-bar' rev='1.2.0'>    <artifact name='$AID' ext='pom'></artifact>  </dependency></code></pre>    <p>布局设置</p>    <pre>  <code class="language-java"><?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.wangchang.testbottomnavigationbar.MainActivity">        <FrameLayout  android:id="@+id/layFrame" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" />        <com.ashokvarma.bottomnavigation.BottomNavigationBar  android:id="@+id/bottom_navigation_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" />  </LinearLayout>  </code></pre>    <p>这个很简单,不用详叙。</p>    <p>主页面实现:</p>    <pre>  <code class="language-java">package com.example.wangchang.testbottomnavigationbar;    import android.support.v4.app.Fragment;  import android.support.v4.app.FragmentManager;  import android.support.v4.app.FragmentTransaction;  import android.support.v7.app.AppCompatActivity;  import android.os.Bundle;    import com.ashokvarma.bottomnavigation.BottomNavigationBar;  import com.ashokvarma.bottomnavigation.BottomNavigationItem;    import java.util.ArrayList;    public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {      private ArrayList<Fragment> fragments;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          BottomNavigationBar bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);          bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);          bottomNavigationBar                  .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC                  );          bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.ic_home_white_24dp, "Home").setActiveColorResource(R.color.orange))                  .addItem(new BottomNavigationItem(R.mipmap.ic_book_white_24dp, "Books").setActiveColorResource(R.color.teal))                  .addItem(new BottomNavigationItem(R.mipmap.ic_music_note_white_24dp, "Music").setActiveColorResource(R.color.blue))                  .addItem(new BottomNavigationItem(R.mipmap.ic_tv_white_24dp, "Movies & TV").setActiveColorResource(R.color.brown))                  .addItem(new BottomNavigationItem(R.mipmap.ic_videogame_asset_white_24dp, "Games").setActiveColorResource(R.color.grey))                  .setFirstSelectedPosition(0)                  .initialise();            fragments = getFragments();          setDefaultFragment();          bottomNavigationBar.setTabSelectedListener(this);      }        /** * 设置默认的 */      private void setDefaultFragment() {          FragmentManager fm = getSupportFragmentManager();          FragmentTransaction transaction = fm.beginTransaction();          transaction.replace(R.id.layFrame, HomeFragment.newInstance("Home"));          transaction.commit();      }        private ArrayList<Fragment> getFragments() {          ArrayList<Fragment> fragments = new ArrayList<>();          fragments.add(HomeFragment.newInstance("Home"));          fragments.add(BookFragment.newInstance("Books"));          fragments.add(MusicFragment.newInstance("Music"));          fragments.add(TvFragment.newInstance("Movies & TV"));          fragments.add(GameFragment.newInstance("Games"));          return fragments;      }        @Override      public void onTabSelected(int position) {          if (fragments != null) {              if (position < fragments.size()) {                  FragmentManager fm = getSupportFragmentManager();                  FragmentTransaction ft = fm.beginTransaction();                  Fragment fragment = fragments.get(position);                  if (fragment.isAdded()) {                      ft.replace(R.id.layFrame, fragment);                  } else {                      ft.add(R.id.layFrame, fragment);                  }                  ft.commitAllowingStateLoss();              }          }        }        @Override      public void onTabUnselected(int position) {          if (fragments != null) {              if (position < fragments.size()) {                  FragmentManager fm = getSupportFragmentManager();                  FragmentTransaction ft = fm.beginTransaction();                  Fragment fragment = fragments.get(position);                  ft.remove(fragment);                  ft.commitAllowingStateLoss();              }          }      }        @Override      public void onTabReselected(int position) {        }  }  </code></pre>    <p>这里主要注意一下Fragment的填充方式,静态工厂构造方法。</p>    <pre>  <code class="language-java">bottomNavigationBar                  .addItem(new BottomNavigationItem(R.drawable.ic_home_white_24dp, "Home"))                  .addItem(new BottomNavigationItem(R.drawable.ic_book_white_24dp, "Books"))                  .addItem(new BottomNavigationItem(R.drawable.ic_music_note_white_24dp, "Music"))                  .addItem(new BottomNavigationItem(R.drawable.ic_tv_white_24dp, "Movies & TV"))                  .addItem(new BottomNavigationItem(R.drawable.ic_videogame_asset_white_24dp, "Games"))                  .initialise();</code></pre>    <p>bottomNavigationBar添加选项。</p>    <pre>  <code class="language-java">  bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){              @Override              public void onTabSelected(int position) {              }              @Override              public void onTabUnselected(int position) {              }              @Override              public void onTabReselected(int position) {              }          });</code></pre>    <p>bottomNavigationBar设置监听选项点击事件setTabSelectedListener。</p>    <p><strong>设置导航栏模式</strong></p>    <blockquote>     <p><strong>Attribute</strong>: bnbMode Values: mode_default, mode_fixed, mode_shifting<br> <strong>Method</strong>: setMode() Values:MODE_DEFAULT, MODE_FIXED, MODE_SHIFTING</p>    </blockquote>    <p>分别是属性或者代码设置</p>    <pre>  <code class="language-java"> bottomNavigationBar  .setMode(BottomNavigationBar.MODE_FIXED);</code></pre>    <p><strong>设置导航栏背景模式</strong></p>    <p><strong>Attribute</strong>: background_style_default, background_style_static, background_style_ripple</p>    <p><strong>Method</strong>:BACKGROUND_STYLE_DEFAULT,BACKGROUND_STYLE_STATIC, BACKGROUND_STYLE_RIPPLE</p>    <pre>  <code class="language-java"> bottomNavigationBar            .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE)      </code></pre>    <p><strong>MODE_FIXED+BACKGROUND_STYLE_STATIC效果</strong></p>    <p><img alt="这里写图片描述" src="https://simg.open-open.com/show/c5059a59bdb83bd513d745e1d1011c7a.gif"></p>    <p><strong>MODE_FIXED+BACKGROUND_STYLE_RIPPLE效果</strong></p>    <p><img alt="这里写图片描述" src="https://simg.open-open.com/show/c198ac09b1a659d7fb21cb1c932011dd.gif"></p>    <p><strong>MODE_SHIFTING+BACKGROUND_STYLE_STATIC效果</strong></p>    <p><img alt="这里写图片描述" src="https://simg.open-open.com/show/556e9db778e6d705a883cdcc02cb1952.gif"></p>    <p><strong>MODE_SHIFTING+BACKGROUND_STYLE_RIPPLE效果</strong></p>    <p><img alt="这里写图片描述" src="https://simg.open-open.com/show/025c5ae048d4cdea1e3b0f4305aad199.gif"></p>    <p>值得一提,模式跟背景的设置都要在添加tab前面,不然不会有效果。</p>    <p><strong>颜色设置</strong></p>    <p><strong>Attributes</strong>: bnbActiveColor, bnbInactiveColor, bnbBackgroundColor Value: Color value or resource<br> <strong>Methods</strong>: setActiveColor, setInActiveColor, setBarBackgroundColor Value: Color value or resource</p>    <pre>  <code class="language-java"> bottomNavigationBar  .setActiveColor(R.color.primary)  .setInActiveColor("#FFFFFF")  .setBarBackgroundColor("#ECECEC")</code></pre>    <p>in-active color : is the icon and text color of the in-active/un-selected tab</p>    <p>是图标和文字的颜色(选中/未选中)</p>    <p>active color : In BACKGROUND_STYLE_STATIC active color is the icon and text color of the active/selected tab. In BACKGROUND_STYLE_RIPPLE active color is the bottom bar background color (which comes with ripple animation)</p>    <p>在BACKGROUND_STYLE_STATIC 模式下颜色是图标和文字的颜色(选中/未选中),在BACKGROUND_STYLE_RIPPLE 模式下是底部导航栏背景色。</p>    <p>background color : In BACKGROUND_STYLE_STATIC background color is the bottom bar background color. In BACKGROUND_STYLE_RIPPLE background color is the icon and text color of the active/selected tab.</p>    <p>在BACKGROUND_STYLE_STATIC 模式下颜色是底部导航栏背景色,BACKGROUND_STYLE_RIPPLE模式下是图标和文字的颜色(选中/未选中)</p>    <p><strong>默认颜色:</strong></p>    <p>Default colors:<br> 1. Theme’s Primary Color will be active color.<br> 2. Color.LTGRAY will be in-active color.<br> 3. Color.WHITE will be background color.</p>    <p>1.主题的PrimaryColor将是active color<br> 2.Color.LTGRAY(灰色)是 in-active color<br> 3.白色是背景色</p>    <p>代码示例:</p>    <pre>  <code class="language-java">        bottomNavigationBar                  .addItem(new BottomNavigationItem(R.drawable.ic_home_white_24dp, "Home").setActiveColor(R.color.orange).setInActiveColor(R.color.teal))                  .addItem(new BottomNavigationItem(R.drawable.ic_book_white_24dp, "Books").setActiveColor("#FFFF00"))                  .addItem(new BottomNavigationItem(R.drawable.ic_music_note_white_24dp, "Music").setInActiveColor("#00FFFF"))                  .addItem(new BottomNavigationItem(R.drawable.ic_tv_white_24dp, "Movies & TV"))                  .addItem(new BottomNavigationItem(R.drawable.ic_videogame_asset_white_24dp, "Games").setActiveColor(R.color.grey))</code></pre>    <p><strong>添加标记</strong></p>    <pre>  <code class="language-java">  BadgeItem numberBadgeItem = new BadgeItem()                  .setBorderWidth(4)                  .setBackgroundColorResource(R.color.blue)                  .setText("5")                  .setHideOnSelect(autoHide.isChecked());         bottomNavigationBar                  .addItem(new BottomNavigationItem(R.drawable.ic_home_white_24dp, "Home").setActiveColorResource(R.color.orange).setBadgeItem(numberBadgeItem))</code></pre>    <p>效果:</p>    <p><img alt="这里写图片描述" src="https://simg.open-open.com/show/4fd91481028580e8f99b3b9bf8eec555.png"></p>    <p>很棒吧,功能很全,效果很nice!</p>    <p>代码下载:<br> <a href="/misc/goto?guid=4959674454039768767">http://pan.baidu.com/s/1eR4DzGY</a></p>    <p>github地址:<br> <a href="/misc/goto?guid=4959674454136873358">https://github.com/Ashok-Varma/BottomNavigation</a></p>    <p> </p>    <p>来自: <a href="/misc/goto?guid=4959674454217816937" rel="nofollow">http://blog.csdn.net/qq_16131393/article/details/51419901</a></p>    <p> </p>