ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 안드로이드 하단 메뉴바 활용하기
    Mobile App/Android 2021. 5. 2. 19:27

    이전 포스팅 : 안드로이드 프래그먼트 활용하기

     

    안드로이드 프래그먼트 활용하기

    MainActivity.java public class MainActivity extends AppCompatActivity { private FragmentManager fragmentManager; private Fragment_Main fragment_main; private Fragment_MyPage fragment_my_page; privat..

    jyspw.tistory.com

     

    사전 작업
    1. res 폴더 우클릭 -> New -> Android Resources Directory -> menu 폴더 생성
    2. res/drawable -> New -> Vector Asset -> 사용할 Clipart 이미지 생성

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
        private FragmentManager fragmentManager;
        private Fragment_Main fragment_main;
        private Fragment_MyPage fragment_my_page;
        private FragmentTransaction fragmentTransaction;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            fragmentManager = getSupportFragmentManager();
    
            fragment_main = new Fragment_Main();
            fragment_my_page = new Fragment_MyPage();
    
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.frameLayout, fragment_my_page).commitNowAllowingStateLoss();
     
     	// 이벤트 리스너 등록
            BottomNavigationView bottomNavigationView = findViewById(R.id.navBar);
            bottomNavigationView.setOnNavigationItemSelectedListener(new MenuClickEventListener()); 
        }
        class MenuClickEventListener implements BottomNavigationView.OnNavigationItemSelectedListener{
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                fragmentTransaction = fragmentManager.beginTransaction();
    
                switch(item.getItemId())
                {
                    case R.id.btn_main:
                        fragmentTransaction.replace(R.id.frameLayout, fragment_main).commitNowAllowingStateLoss();
                        break;
                    case R.id.btn_my_page:
                        fragmentTransaction.replace(R.id.frameLayout, fragment_my_page).commitNowAllowingStateLoss();
                        break;
                }
                return true;
            }
        }
    
        /* 
        // 이전 포스팅 코드
        public void MenuClickHandler(View v){
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
    
            switch (v.getId()){
                case R.id.btn_main:
                    fragmentTransaction.replace(R.id.frameLayout, fragment_main).commitNowAllowingStateLoss();
                    break;
                case R.id.btn_my_page:
                    fragmentTransaction.replace(R.id.frameLayout, fragment_my_page).commitNowAllowingStateLoss();
                    break;
            }
        }*/
    }

     

    res/menu/nav.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:id="@+id/btn_main"
            android:icon="@drawable/ic_baseline_home_24"
            android:title="Main" />
        <item
            android:id="@+id/btn_my_page"
            android:icon="@android:drawable/ic_menu_manage"
            android:title="My Page" />
    </menu>

     

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        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">
    
        <FrameLayout
            android:id="@+id/frameLayout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@id/navBar"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
        </FrameLayout>
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/navBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="parent"
            app:menu="@menu/nav" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

    최종 결과물

    댓글 0

Designed by Tistory.