簡單粗暴的MaterialDesign風(fēng)格的底部導(dǎo)航BottomNavigationView

請(qǐng)注明出處http://blog.csdn.net/qq_23179075/article/details/64905520

MaterialDesign風(fēng)格效果:

導(dǎo)航1.gif

常規(guī)效果:

導(dǎo)航2.gif

使用方法請(qǐng)見Github:https://github.com/armcha/LuseenBottomNavigation

這里貼上面效果的代碼:

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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bottomnavigationview.MainActivity">
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_above="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    <com.luseen.luseenbottomnavigation.BottomNavigation.BottomNavigationView
        android:id="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:bnv_colored_background="false"
        app:bnv_with_text="true"
        app:bnv_shadow="true"
        app:bnv_tablet="false"
        app:bnv_viewpager_slide="true"
        app:bnv_active_color="@color/colorPrimary"
        app:bnv_active_text_size="@dimen/bottom_navigation_text_size_active"
        app:bnv_inactive_text_size="@dimen/bottom_navigation_text_size_inactive"
        />
</RelativeLayout>

fragment_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_meinv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>

</RelativeLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.pager)
    ViewPager pager;
    @BindView(R.id.bottomNavigation)
    BottomNavigationView bottomNavigation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * 讓狀態(tài)了成半透明狀態(tài)
         */
        this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = this.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);//設(shè)置狀態(tài)欄顏色透明
//            window.setNavigationBarColor(Color.TRANSPARENT);//設(shè)置導(dǎo)航欄顏色透明
        }
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initViews();
        initListener();
    }
    private void initListener() {
        //pager切換監(jiān)聽
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                bottomNavigation.selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //導(dǎo)航選擇監(jiān)聽
        bottomNavigation.setOnBottomNavigationItemClickListener(new OnBottomNavigationItemClickListener() {
            @Override
            public void onNavigationItemClick(int index) {
                pager.setCurrentItem(index);
            }
        });
    }
    private void initViews() {
        BottomNavigationItem bottomNavigationItem1 = new BottomNavigationItem
                ("主頁", ContextCompat.getColor(this, R.color.color02), R.drawable.ic_home);
        BottomNavigationItem bottomNavigationItem2 = new BottomNavigationItem
                ("關(guān)注", ContextCompat.getColor(this, R.color.color03), R.drawable.ic_guanzhu);
        BottomNavigationItem bottomNavigationItem3 = new BottomNavigationItem
                ("消息", ContextCompat.getColor(this, R.color.color09), R.drawable.ic_msg);
        BottomNavigationItem bottomNavigationItem4 = new BottomNavigationItem
                ("我的", ContextCompat.getColor(this, R.color.color05), R.drawable.ic_my);
        bottomNavigation.addTab(bottomNavigationItem1);
        bottomNavigation.addTab(bottomNavigationItem2);
        bottomNavigation.addTab(bottomNavigationItem3);
        bottomNavigation.addTab(bottomNavigationItem4);
        bottomNavigation.willNotRecreate(true);
        bottomNavigation.setOnBottomNavigationItemClickListener(new OnBottomNavigationItemClickListener() {
            @Override
            public void onNavigationItemClick(int index) {
                System.out.println("Item " + index + " clicked");
            }
        });
        pager.setAdapter(new BaseFragmentAdapter(getSupportFragmentManager()));
    }
}

BaseFragment.java

public class BaseFragment extends Fragment {

    public static BaseFragment newInstance(int s) {
        BaseFragment fragment = new BaseFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("s", s);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_fragment,container,false);
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_meinv);
        imageView.setImageResource(getArguments().getInt("s"));
        return view;
    }
}

BaseFragmentAdapter.java

public class BaseFragmentAdapter extends FragmentPagerAdapter {
    private List<String> fragments;
    private int [] Imgs= {
        R.mipmap.mn1,
        R.mipmap.mn2,
        R.mipmap.mn3,
        R.mipmap.mn4,
    };

    public BaseFragmentAdapter(FragmentManager fm) {
        super(fm);
        this.fragments= new ArrayList<>();
        fragments.add("首頁");
        fragments.add("關(guān)注");
        fragments.add("消息");
        fragments.add("我的");
    }

    @Override
    public Fragment getItem(int position) {
        return BaseFragment.newInstance(Imgs[position]);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return fragments.get(position);
    }
}

不知到為什么我通過下面這種方式與ViewPager聯(lián)動(dòng)的時(shí)候,導(dǎo)航的圖標(biāo)顏色總是不對(duì),如果有使用過這個(gè)的童鞋還希望指點(diǎn)!見笑了

ContextCompat.getColor(context, R.color.firstColor)
 bottomNavigationView.setUpWithViewPager(yourPager , colorResources , imageResources);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,828評(píng)論 25 709
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,842評(píng)論 2 45
  • 1.內(nèi)置指令 什么是指令? HTML在構(gòu)建應(yīng)用(App)時(shí)存在諸多不足之處,AngularJS通過擴(kuò)展一系列的HT...
    miner敏兒閱讀 398評(píng)論 0 0
  • 你如果沒有處事圓滑的能力,就不要收受賄賂。 你如果沒寬廣的胸懷,就不要做君子。你如果做不到清心寡欲就不要做圣人。跳...
    aa60bdf93b0f閱讀 393評(píng)論 0 1
  • 轉(zhuǎn)眼間,九月就成了過去式,這個(gè)月,其實(shí)對(duì)我而言有些漫長,有些煎熬,但是沒想到,最后的結(jié)果,好像并沒想象中的糟糕。 ...
    浪漫的高貴閱讀 397評(píng)論 8 12

友情鏈接更多精彩內(nèi)容