底部导航,BottomNavigationBar

依赖:
compile 'com.ashokvarma.android:bottom-navigation-bar:2.0.4'
布局:

<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=".MainActivity">

    <RelativeLayout
        android:id="@+id/rel"

        android:layout_width="match_parent"
        android:layout_height="match_parent">

    RelativeLayout>


    <com.ashokvarma.bottomnavigation.BottomNavigationBar

        android:id="@+id/bottom_navigation_bar"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom" />


RelativeLayout>
主页面:
package com.wode.bottomnavigationbar;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.RelativeLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.rel)
    RelativeLayout rel;
    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar bottomNavigationBar;
    private Fragmentone fragmentone;
    private Fragmenttwo fragmenttwo;
    private Fragmentthree fragmentthree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        fragmentone = new Fragmentone();
        fragmenttwo = new Fragmenttwo();
        fragmentthree = new Fragmentthree();
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED)

                .setActiveColor("#ff0000") //选中颜色
                .setInActiveColor("#000000") //未选中颜色
                .setBackgroundResource(R.drawable.bottom_bg);

        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);

        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.found, "精选"))
                .addItem(new BottomNavigationItem(R.drawable.fancy, "发现"))
                .addItem(new BottomNavigationItem(R.drawable.my, "我的"))
                .initialise();

        getSupportFragmentManager().beginTransaction()
                .add(R.id.rel,fragmentone)
                .add(R.id.rel,fragmenttwo)
                .add(R.id.rel,fragmentthree)
              .commit();
        getSupportFragmentManager().beginTransaction()
                .show(fragmentone)
                .hide(fragmenttwo)
                .hide(fragmentthree)
               .commit();
        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){

            private FragmentTransaction fragmentTransaction;

            @Override
            public void onTabSelected(int position) {

                switch (position){

                    case 0:
                        getSupportFragmentManager().beginTransaction()
                                .show(fragmentone)
                                .hide(fragmenttwo)
                                .hide(fragmentthree)
                                .commit();
                        break;
                    case 1:
                        getSupportFragmentManager().beginTransaction()
                                .hide(fragmentone)
                                .show(fragmenttwo)
                                .hide(fragmentthree)
                                .commit();
                        break;
                    case 2:
                        getSupportFragmentManager().beginTransaction()
                                .hide(fragmentone)
                                .hide(fragmenttwo)
                                .show(fragmentthree)
                                .commit();
                        break;

                };
            }
            @Override
            public void onTabUnselected(int position) {
            }
            @Override
            public void onTabReselected(int position) {
            }
        });


    }
}

你可能感兴趣的:(底部导航,BottomNavigationBar)