具有FloatingActionButton的Bottomnavigationview

时间:2018-07-08 18:58:33

标签: android android-layout floating-action-button bottomnavigationview

如何创建此Bottomnavigationview?我使用AHBottomNavigation成功创建了此视图,并添加了FloatingActionButton以底部边缘进行查看。但是我认为添加保证金来固定FloatingActionButton位置并不是正确的。在某些情况下,例如Recyclerview滚动时,我想隐藏底部导航和FloatingActionButton。enter image description here

1 个答案:

答案 0 :(得分:0)

 <android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="?android:attr/windowBackground"
    android:foreground="?attr/selectableItemBackground"
    app:itemBackground="@color/background"
    app:itemIconTint="@android:color/holo_green_dark"
    app:itemTextColor="@android:color/black"
    app:menu="@menu/more_nav"/>

在您的Java类中使用此代码

BottomNavigationView navigation = findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    // attaching bottom sheet behaviour - hide / show on scroll
    CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) navigation.getLayoutParams();
    layoutParams.setBehavior(new BottomNavigationBehavior());
    }
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment;
        switch (item.getItemId()) {
            case R.id.inbox:
                getSupportActionBar().setTitle("Inbox");
                fragment = new OneFragment();
                loadFragment(fragment);
                return true;
            case R.id.terms:
                getSupportActionBar().setTitle("Terms");
                fragment = new TwoFragment();
                loadFragment(fragment);
                return true;
            case R.id.upgrade:
                getSupportActionBar().setTitle("Upgrade Plan");
                fragment = new ThreeFragment();
                loadFragment(fragment);
                return true;
           case R.id.bot:
               getSupportActionBar().setTitle("Buyfie Chatbot");
               fragment = new FourFragment();
               loadFragment(fragment);
               return true;
        }

        return false;
    }
};
private void loadFragment(Fragment fragment) {
    // load fragment
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_container, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

在您的主要活动中使用它

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />