【问题标题】:Bottomnavigationview with FloatingActionButton带有 FloatingActionButton 的底部导航视图
【发布时间】:2018-12-16 12:34:48
【问题描述】:

如何创建这个底部导航视图?我使用AHBottomNavigation 成功创建了这个视图,并添加了 FloatingActionButton 以查看底部边距。但我认为添加边距来修复 FloatingActionButton 位置是不正确的。我想在 Recyclerview 滚动等某些情况下隐藏底部导航和 FloatingActionButton。

【问题讨论】:

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


    【解决方案1】:
     <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" />
    

    【讨论】:

    • BottomNavigationView 的中心项和 FloationgActionButton 怎么样?
    • 您能否提供更多详细信息?特别是关于OP关于FAB实施的问题?
    猜你喜欢
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多