Android 使用BottomNavigationView實現底部導航欄

lvxiangan發表於2018-07-25

今天我們來使用BottomNavigationView來實現android底部導航欄,在Android Support Library 25 中增加了 BottomNavigationView 控制元件,官方為我們提供了這樣這一個控制元件,就來試試吧!

1.效果圖如下:

這裡寫圖片描述

2.匯入以下support:design library,BottomNavigationView就在這個design庫中。

compile'com.android.support:design:25.0.1'
  •  

3.在res下新建一個menu,然後在menu下建一個navigation.xml(在四個item中,放置了四個icon)。 
navigation.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/iv1_p"
        android:title="首頁" />
    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/iv2_p"
        android:title="錢包" />
    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/iv3_p"
        android:title="卡片" />
    <item
        android:id="@+id/navigation_person"
        android:icon="@drawable/iv4_p"
        android:title="個人" />
</menu>
  •  

4.在佈局檔案中使用BottomNavigationView:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.afanbaby.bottomnavigationdemo.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:itemBackground="@null"
        app:itemIconTint="@drawable/bottom_navigation_selector"
        app:itemTextColor="@drawable/bottom_navigation_selector"
        app:menu="@menu/navigation" />

</LinearLayout>

5.在drawable中新建bottom_navigation_selector.xml(設定文字和icon選中和未選中的顏色):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/tab_checked" android:state_checked="true" />
    <item android:color="@color/tab_unchecked" android:state_checked="false" />
</selector>

6.color中增加兩個顏色值:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tab_checked">#ED6A2C</color>
        <color name="tab_unchecked">#757575</color>
</resources>

7.就可以在MainActivity中使用了:


public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNavigationView;
    private ViewPagerAdapter viewPagerAdapter;
    private ViewPager viewPager;
    private MenuItem menuItem;

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

        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
        viewPager = (ViewPager) findViewById(R.id.vp);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (menuItem != null) {
                    menuItem.setChecked(false);
                } else {
                    bottomNavigationView.getMenu().getItem(0).setChecked(false);
                }
                menuItem = bottomNavigationView.getMenu().getItem(position);
                menuItem.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(viewPagerAdapter);
        List<Fragment> list = new ArrayList<>();
        list.add(TestFragment.newInstance("首頁"));
        list.add(TestFragment.newInstance("錢包"));
        list.add(TestFragment.newInstance("卡片"));
        list.add(TestFragment.newInstance("個人"));
        viewPagerAdapter.setList(list);

    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            menuItem = item;
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    viewPager.setCurrentItem(0);
                    return true;
                case R.id.navigation_dashboard:
                    viewPager.setCurrentItem(1);
                    return true;
                case R.id.navigation_notifications:
                    viewPager.setCurrentItem(2);
                    return true;
                case R.id.navigation_person:
                    viewPager.setCurrentItem(3);
                    return true;
            }
            return false;
        }
    };
}

8.ViewPagerAdapter:


public class ViewPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> list;

    public void setList(List<Fragment> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }
}

9.測試的TestFragment:

public class TestFragment extends Fragment {

    private TextView tv;

    public static TestFragment newInstance(String name) {

        Bundle args = new Bundle();
        args.putString("name", name);
        TestFragment fragment = new TestFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_test, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tv = (TextView) view.findViewById(R.id.fragment_test_tv);

        Bundle bundle = getArguments();
        if (bundle != null) {
            String name = bundle.get("name").toString();
             tv.setText(name);
        }

    }

}

10.測試的TestFragment的佈局檔案:

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

    <TextView
        android:id="@+id/fragment_test_tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="28dp"
        android:gravity="center"
        android:text="ceshi"/>

</LinearLayout>

這樣就完成了,是不是很簡單,快去試下吧!

11.需要注意的地方: 
(1)取消位移動畫,如果你的選單數大於3個,則介面是這樣的(引用網路)。 

這裡寫圖片描述

這個效果可定不是我們想要的效果,可以通過反射解決。 
新建一個BottomNavigationViewHelper.class:

public class BottomNavigationViewHelper {

    public static void disableShiftMode(BottomNavigationView view) {

        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);

        try {
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                //noinspection RestrictedApi
                item.setShiftingMode(false);
                // set once again checked value, so view will be updated
                //noinspection RestrictedApi
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("BNVHelper", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BNVHelper", "Unable to change value of shift mode", e);
        }
    }
}

(2)取消導航欄的點選效果(類似水波紋的效果)

這裡寫圖片描述

我們只需在BottomNavigationView的佈局檔案中新增一個屬性:

app:itemBackground="@null"
  • (3)取消導航欄的每項點選文字和圖片放大的效果

這裡寫圖片描述

我們需要在values中的demens.xml中設定:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--BottomNavigationView 的選中沒有選中的字型大小-->
    <dimen name="design_bottom_navigation_active_text_size">10dp</dimen>
    <dimen name="design_bottom_navigation_text_size">10dp</dimen>

    <!--BottomNavigationView 只放圖示時的設定-->
    <!--<dimen name="design_bottom_navigation_active_text_size">0dp</dimen>-->
    <!--<dimen name="design_bottom_navigation_text_size">0dp</dimen>-->
    <!--<dimen name="design_bottom_navigation_margin">16dp</dimen>-->
</resources>

12.demo的地址:

http://download.csdn.net/download/afanbaby/10237265

相關文章