하단네비게이션

 

 

1. gradle 추가

2. activity_main.xml

3. menu 폴더 생성

4. bottom_menu.xml

5. java파일 및 xml 생성

6. MainActivity


1. gradle 추가

implementation 'com.android.support:design:28.0.0'

 

build.gradle에 implementation 추가시켜줍니다.

버전이 맞지 않으면 에러가 발생할 수 있으므로 버전을 맞춰주세요.

 

2. 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/main_frame"
        android:layout_width="match_parent"
        android:layout_height="659dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavi"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavi"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:itemIconTint="#000"
        app:itemTextColor="#000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/bottom_menu"
        tools:layout_editor_absoluteX="8dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

3. menu 폴더 생성

res - New - Android Resource Directory에서 Resource type을 menu로 선택 후 생성.

 

4. bottom_menu.xml 생성

menu 폴더에 bottom_menu.xml 생성 후 메뉴 추가.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/show"
        android:enabled="true"
        android:icon="@drawable/ic_import_contacts_black_24dp"
        android:title="보기"/>

    <item
        android:id="@+id/write"
        android:enabled="true"
        android:icon="@drawable/ic_create_black_24dp"
        android:title="쓰기"/>

    <item
        android:id="@+id/info"
        android:enabled="true"
        android:icon="@drawable/ic_info_outline_black_24dp"
        android:title="정보"/>
</menu>

 

5. java파일 및 xml 생성

각각의 메뉴마다 연결시킬 파일 만들기

 

- Fragment1.java

public class Fragment1 extends Fragment {

    private View view;

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



        return view;
    }
}

- fragment1.xml

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="30sp"
        android:text="fragment1"/>

</LinearLayout>

위와 같이 2와 3에 해당하는 java파일 & xml 파일 추가 생성~!

 

 

6. MainActivity

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNavigationView; // 바텀네비게이션 뷰
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private Fragment1 fragment1;
    private Fragment2 fragment2;
    private Fragment3 fragment3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavi);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()){

                    case R.id.show:
                        setFrag(0);
                        break;
                    case R.id.write:
                        setFrag(1);
                        break;
                    case R.id.info:
                        setFrag(2);
                        break;
                }
                return true;
            }
        });

        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();

        setFrag(0); // 첫화면 설정
    }

    // 프래그먼트 교체가 일어나는 메서드
    private void setFrag(int n){

        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();

        switch (n){
            case 0:
                transaction.replace(R.id.main_frame, fragment1);
                transaction.commit();
                break;
            case 1:
                transaction.replace(R.id.main_frame, fragment2);
                transaction.commit();
                break;
            case 2:
                transaction.replace(R.id.main_frame, fragment3);
                transaction.commit();
                break;
        }
    }
}

+ Recent posts