本文實例為大家分享了Android實現(xiàn)底部導(dǎo)航欄的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、召陵網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5響應(yīng)式網(wǎng)站、購物商城網(wǎng)站建設(shè)、集團公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為召陵等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
展示
MODE_FIXED+BACKGROUND_STYLE_STATIC效果
DE_FIXED+BACKGROUND_STYLE_RIPPLE效果
MODE_SHIFTING+BACKGROUND_STYLE_STATIC效果
MODE_SHIFTING+BACKGROUND_STYLE_RIPPLE效果
1在Gradle中添加
compile ‘com.ashokvarma.android:bottom-navigation-bar:0.9.5'
2布局實現(xiàn)
3類中Activity中添加BottomNavigationItem
BottomNavigationBar bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar); 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();
4設(shè)置事件監(jiān)聽器TabChangeListener
bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){ @Override public void onTabSelected(int position) { } @Override public void onTabUnselected(int position) {] } @Override public void onTabReselected(int position) { } });
5.代碼實現(xiàn)
package com.android.xiaobai; import android.os.Bundle; import android.support.annotation.Nullable; 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.util.Log; import com.ashokvarma.bottomnavigation.BottomNavigationBar; import com.ashokvarma.bottomnavigation.BottomNavigationItem; import java.util.ArrayList; import fragment.ContactsFragment; import fragment.DongtaiFragment; import fragment.MsgFragment; /** * Created by xiaobai on 2018/1/31/031. */ public class MessageActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener { private ArrayListfragments; private BottomNavigationBar bottomNavigationBar; private MsgFragment msgFragment; private ContactsFragment contactsFragment; private DongtaiFragment dongtaiFragment; private FragmentManager fragmentManager; private FragmentTransaction transaction; int lastSelectedPosition = 0; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activty_message); bottomNavigationBar = findViewById(R.id.bottom_navigation_bar); init(); } private void init() { //要先設(shè)計模式后再添加圖標! //設(shè)置按鈕模式 MODE_FIXED表示固定 MODE_SHIFTING表示轉(zhuǎn)移 bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED); //設(shè)置背景風格 // BACKGROUND_STYLE_STATIC表示靜態(tài)的 //BACKGROUND_STYLE_RIPPLE表示漣漪的,也就是可以變化的 ,跟隨setActiveColor里面的顏色變化 bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE); //添加并設(shè)置圖標、圖標的顏色和文字 bottomNavigationBar .addItem(new BottomNavigationItem(R.drawable.im_botton_message, "消息")).setActiveColor(R.color.blue) .addItem(new BottomNavigationItem(R.drawable.im_botton_contacts, "聯(lián)系人")).setActiveColor(R.color.red) .addItem(new BottomNavigationItem(R.drawable.im_botton_dongtai, "動態(tài)")).setActiveColor(R.color.orign) .setFirstSelectedPosition(lastSelectedPosition ) .initialise(); bottomNavigationBar.setTabSelectedListener(this); setDefaultFragment(); } //設(shè)置初始界面 private void setDefaultFragment() { fragmentManager = getSupportFragmentManager(); transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.layFrame, MsgFragment.newInstance("消息")); transaction.commit(); } @Override public void onTabSelected(int position) { fragmentManager = getSupportFragmentManager(); transaction = fragmentManager.beginTransaction(); switch (position) { case 0: if (msgFragment == null) { msgFragment = MsgFragment.newInstance("消息"); } transaction.replace(R.id.layFrame, msgFragment); break; case 1: if (contactsFragment == null) { contactsFragment = ContactsFragment.newInstance("聯(lián)系人"); } transaction.replace(R.id.layFrame, contactsFragment); break; case 2: if (dongtaiFragment == null) { dongtaiFragment = DongtaiFragment.newInstance("動態(tài)"); } transaction.replace(R.id.layFrame, dongtaiFragment); break; default: break; } // 事務(wù)提交 transaction.commit(); } @Override public void onTabUnselected(int position) { Log.d("dongtaiFragment", "onTabUnselected() called with: " + "position = [" + position + "]"); } @Override public void onTabReselected(int position) { } }
package fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.android.xiaobai.R; /** * Created by xiaobai on 2018/1/31/031. */ public class MsgFragment extends BaseFragment { /** * 標志位,標志已經(jīng)初始化完成 */ private boolean isPrepared; /** * 是否已被加載過一次,第二次就不再去請求數(shù)據(jù)了 */ private boolean mHasLoadedOnce; TextView textView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (mView == null) { // 需要inflate一個布局文件 填充Fragment mView = inflater.inflate(R.layout.fragment_msg, container, false); initView(); isPrepared = true; // 實現(xiàn)懶加載 lazyLoad(); } //緩存的mView需要判斷是否已經(jīng)被加過parent, 如果有parent需要從parent刪除,要不然會發(fā)生這個mView已經(jīng)有parent的錯誤。 ViewGroup parent = (ViewGroup) mView.getParent(); if (parent != null) { parent.removeView(mView); } return mView; } /** * 初始化控件 */ private void initView() { } @Override public void lazyLoad() { if (!isPrepared || !isVisible || mHasLoadedOnce) { return; } //填充各控件的數(shù)據(jù) mHasLoadedOnce = true; } public static MsgFragment newInstance(String param1) { MsgFragment fragment = new MsgFragment(); Bundle args = new Bundle(); args.putString("agrs1", param1); fragment.setArguments(args); return fragment; } }
<?xml version="1.0" encoding="utf-8"?>
package fragment; import android.support.v4.app.Fragment; import android.view.View; /** * Created by xiaobai on 2018/1/31/031. */ public abstract class BaseFragment extends Fragment { /** * Fragment當前狀態(tài)是否可見 */ public boolean isVisible; /** * inflate布局文件 返回的view */ public View mView; /** * 簡化 findViewById * * @param viewId * @param* @return */ protected T find(int viewId) { return (T) mView.findViewById(viewId); } /** * setUserVisibleHint是在onCreateView之前調(diào)用的 * 設(shè)置Fragment可見狀態(tài) */ @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); /** * 判斷是否可見 */ if (getUserVisibleHint()) { isVisible = true; onVisible(); } else { isVisible = false; onInvisible(); } } /** * 可見 */ private void onVisible() { lazyLoad(); } /** * 不可見 */ private void onInvisible() { } /** * 延遲加載 * 子類必須重寫此方法 */ public abstract void lazyLoad(); }
BottomNavigationBar 圖標和文字的間距:
在自己項目里value文件夾中的dimens.xml里面復(fù)寫fixed_height_bottom_padding(默認是10dp,值越小,間距越大。適合的模式是BottomNavigationBar.MODE_FIXED)
如果模式是BottomNavigationBar.MODE_SHIFTING也是一樣,復(fù)寫和修改相關(guān)的參數(shù)
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。