BottomNavigationView

BottomNavigation is Menu.xml

Everyone page is Fragment

Step1: Create

Create a menu directFolder and create menu_nav.xml

everyone item need icon . id . title

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/ic_home"
        android:title="home" />
    <item
        android:id="@+id/nav_dashboard"
        android:icon="@drawable/ic_dashboard"
        android:title="dashboard" />
    <item
        android:id="@+id/nav_notify"
        android:icon="@drawable/ic_notify"
        android:title="notify" />
</menu>

Create NavigationView on activity_main.xml and add attribute menu in navigation

<android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/menu_bottomnav"
 />

Step2:Write Code

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int id = menuItem.getItemId();
                switch (id){

                    case R.id.nav_home:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.fragment_container,HomeFragment.getInstance())
                                .commit();
                        break;
                    case R.id.nav_dashboard:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.fragment_container,DashboardFragment.getInstance())
                                .commit();
                        break;
                    case R.id.nav_notify:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.fragment_container,NotifyFragment.getInstance())
                                .commit();
                        break;
                }

                return true;
            }
        });
    }

 
}

Note

onNavigationItemSelected  must be turn true , the select with hight light

Step3:How many menu_item , How many Fragment

Create Home , Dashboard , Notify Fragment , write singleton and override OnCreatedView

just like this : Fragment

Code

https://github.com/encorex32268/blogthing/tree/master/TestBottomNavigation/app/src/main/java/com/lee/testbottomnavigation

Result:

With FloatingActionButton BottomCenter

Pull FloatingButton on BottomCenter , set Constraint , set elevation to Max

Fix menu.xml  , create empty item to center

<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/nav2_home"
        android:icon="@drawable/ic_home"
        android:title="home" />
    <item
        android:enabled="false"
        android:title=" " />
    <item
        android:id="@+id/nav2_person"
        android:icon="@drawable/ic_person"
        android:title="person" />
</menu>

Result:

 

 

最後修改日期: 2019-04-30 18:57:50

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料