๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - ํ•˜๋‹จ ํƒญ๋ฐ”, ๋„ค๋น„๊ฒŒ์ด์…˜ ๋ฐ” ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• BottomNavigationView ๋ณธ๋ฌธ

๐Ÿค–Android

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - ํ•˜๋‹จ ํƒญ๋ฐ”, ๋„ค๋น„๊ฒŒ์ด์…˜ ๋ฐ” ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• BottomNavigationView

๋ฃจ๋ฆฌ์•ผใ…‘ 2024. 1. 9. 18:01
๋ฐ˜์‘ํ˜•

์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ๊ฐœ๋ฐœ ์‹œ ํ•˜๋‹จ ํƒญ๋ฐ”๋ฅผ ์–ด๋ ต์ง€ ์•Š๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
ํƒญ๋ฐ”๋Š” ์ฃผ๋กœ ๋„ค๋น„๊ฒŒ์ด์…˜์˜ ๊ธฐ๋Šฅ์„ํ•˜์—ฌ ์‚ฌ์šฉ์ž์˜ ํŽธ์˜๋ฅผ ๋•๋Š”๋‹ค.

 

1. build.gradle.kts  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

์ตœ์‹ ๋ฒ„์ „์œผ๋กœ ์—…๊ทธ๋ ˆ์ด๋“œ ๋˜์–ด ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋‹ˆ
๋ฒ„์ „ ๋ฒˆํ˜ธ๋ฅผ ํ™•์ธํ•˜์—ฌ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

dependencies {

    implementation("androidx.navigation:navigation-fragment:2.7.6")
    implementation("androidx.navigation:navigation-ui:2.7.6")
}

 

2. activity_main.xml

2.1. ๋ ˆ์ด์•„์›ƒ ๋ณ€๊ฒฝ

ConstraintLayout์„ RelativeLayout์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
    
</RelativeLayout>

 

2.2. BottomNavigationView ์ƒ์„ฑ

๋„ค๋น„๊ฒŒ์ด์…˜์ด ๋ณด์ผ BottomNavigationView์„ ์ƒ์„ฑํ•˜์—ฌ
๋ ˆ์ด์•„์›ƒ ์Šคํƒ€์ผ ๊ฐ’์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ง€์ •ํ•ด์ค€๋‹ค.

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FF5252"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#FF5252" />

</RelativeLayout>

 

3.Navigation Resource File ์ƒ์„ฑ

Resource Manager-Navigation- + ํ”Œ๋Ÿฌ์Šค ๋ฒ„ํŠผ ํด๋ฆญ

๋ฆฌ์†Œ์Šค ํŒŒ์ผ ์ด๋ฆ„ ์„ค์ •-OK

์ƒ์„ฑ๋œ ํŒŒ์ผ ํด๋ฆญ

 

Create new destination

Fragment (Blank) ์„ ํƒ

 

Fargment Name ์„ค์ •

 

์ด๋ ‡๊ฒŒ ์ด ๋„ค๋น„๊ฒŒ์ด์…˜ ์•„์ด์ฝ˜ ์ˆ˜ ๋งŒํผ ๋งŒ๋“ ๋‹ค.
๋ณธ์ธ์€ ์ด 3๊ฐœ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

 

 

4. activity_main.xml Fragment  ๋ฐฐ์น˜

activity_main.xml์—์„œ NavHostFragment๋ฅผ
RelativeLayout์— ๋Œ์–ด๋‹ค ๋†“๋Š”๋‹ค.

Navigation Graphs์—์„œ ์•„๊นŒ ์ƒ์„ฑํ•œ my_nav.xml๋ฅผ ์„ ํƒํ•œ๋‹ค.

 

activity_main.xml์—์„œ ์ด Fragment์˜ id์™€ ์œ„์น˜(layout_avove)๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค.

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/bottomNavigationView"
    app:navGraph="@navigation/my_nav"/>

 

5. Menu ๋งŒ๋“ค๊ธฐ

Resource Manager์—์„œ ๋”๋ณด๊ธฐ ๋ˆŒ๋Ÿฌ Menu ์„ ํƒ ํ›„
+ ๋ฒ„ํŠผ ๋ˆŒ๋Ÿฌ์„œ Menu Resource File์„ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค.

 

ํŒŒ์ผ๋ช… ์„ค์ • ํ›„ OK

 

์˜ค๋ฅธ์ชฝ ์ƒ๋‹จ ๋””์ž์ธ ๋ณด๊ธฐ๋กœ ๋ฐ”๊พธ๊ณ 
ํƒญ์ด 3๊ฐœ์ด๋ฏ€๋กœ ๋ฉ”๋‰ด์•„์ดํ…œ 3๊ฐœ ์ถ”๊ฐ€ํ•œ๋‹ค.

item๋“ค์˜ Common Attributes๋ฅผ id, title, icon์„ ์„ค์ •ํ•ด์ค€๋‹ค.

์•„์ด์ฝ˜ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

https://ruriruriya.tistory.com/147

 

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - ๋ฉ”๋‰ด ์•„์ด์ฝ˜ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•(Vector Asset)

์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ๊ฐœ๋ฐœ ์‹œ ์•„์ด์ฝ˜์ด ํ•„์š”ํ•œ๋ฐ ์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค์—์„œ๋Š” ๊ฐ„๋‹จํ•œ ์•„์ด์ฝ˜์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์•„์ฃผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ƒ‰์ƒ, ํฌ๊ธฐ ๋ณ€๊ฒฝ ๋“ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 1. [File]-[New]-[Vector Asset] 2. ํด๋ฆฝ์•„ํŠธ๋กœ ๋งŒ๋“ค

ruriruriya.tistory.com

 

6. activity_main.xml์—์„œ BottomNavigationView ๋ฉ”๋‰ด ์„ธํŒ…

label์„ ๋ณด์—ฌ์ฃผ๊ธฐ ์—ฌ๋ถ€๋ฅผ ์„ค์ •
app:labelVisibilityMode="unlabeled"

menu ์— ์•„๊นŒ ๋งŒ๋“  Menu Resource ์ง€์ •.
app:menu="@menu/bottom_menu"

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottomNavigationView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#FF5252"
    app:labelVisibilityMode="unlabeled"
    app:menu="@menu/bottom_menu" />

 

7. MainActivity.java ์„ธํŒ…

์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋กœ ์„ธํŒ…ํ•œ๋‹ค.

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

public class MainActivity extends AppCompatActivity {

    // ์•„๋ž˜ ์•„์ด์ฝ˜ ๋‚˜์˜ค๋Š” ์˜์—ญ
    BottomNavigationView bottomNavigationView;

    // ๊ฐ ํ”„๋ ˆ๊ทธ๋จผํŠธ๋ฅผ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋กœ ๋งŒ๋“ ๋‹ค.
    Fragment firstFragment;
    Fragment secondFragment;
    Fragment thirdFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNavigationView = findViewById(R.id.bottomNavigationView);

        firstFragment = new FirstFragment();
        secondFragment = new SecondFragment();
        thirdFragment = new ThirdFragment();

        bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int itemId = item.getItemId();

                Fragment fragment = null;
                
                if(itemId == R.id.firstFragment){

                    fragment = firstFragment;
                    
                } else if (itemId == R.id.secondFragment) {

                    fragment = secondFragment;
                    
                } else if (itemId == R.id.thirdFragment) {

                    fragment = thirdFragment;
                    
                }


                return loadFragment(fragment);
            }
        });


    }

    boolean loadFragment(Fragment fragment){
        if(fragment != null){
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragment,fragment)
                    .commit();
            return true;
        }else {
            return false;
        }
    }
}

 

8. ํ™”๋ฉด ํ™•์ธ

๋ฐ˜์‘ํ˜•