Android

์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค ํƒญ ๋ฐ”(Tab Bar) ๊ตฌํ˜„ํ•˜๊ธฐ

567Rabbit 2024. 6. 18. 10:51

 

์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค Activity์™€ Fragment์˜ ์ฐจ์ด 

 

 

Activity

 

์•ฑ์ด ์‹คํ–‰๋˜๋ฉด ์ฒซ ๋ฒˆ์งธ๋กœ ํ˜ธ์ถœ๋˜๋Š” ์ปดํฌ๋„ŒํŠธ,

 

ํ™”๋ฉด ์ „์ฒด๋ฅผ ์ฐจ์ง€ํ•˜๋ฉฐ, ๋ชจ๋“  UI ์š”์†Œ๋ฅผ ํฌํ•จํ•œ๋‹ค

 

 

vs

 

 

Fragment

 

ex) ํƒœ๋ธ”๋ฆฟ์˜ ๋‘ ํŒจ๋„ ํ™”๋ฉด(์™ผ์ชฝ ๋ชฉ๋ก, ์˜ค๋ฅธ์ชฝ ์ƒ์„ธ ๋ณด๊ธฐ), ํƒญ ๋ทฐ์˜ ๊ฐ ํƒญ ๋“ฑ.

 

Activity ๋‚ด์—์„œ ๋ถ€๋ถ„์ ์œผ๋กœ UI๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ปดํฌ๋„ŒํŠธ

 

Activity์˜ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋กœ, ์—ฌ๋Ÿฌ Fragment๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ํ•˜๋‚˜์˜ Activity๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค.

 

ํ™”๋ฉด์˜ ์ผ๋ถ€๋งŒ์„ ์ฐจ์ง€ํ•˜๋ฉฐ, ์—ฌ๋Ÿฌ Fragment๊ฐ€ ํ•˜๋‚˜์˜ Activity ๋‚ด์—์„œ ๋™์‹œ ์ž‘๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Fragment๋Š” ์ž์‹ ์˜ ์ˆ˜๋ช… ์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ง€์ง€๋งŒ, ์ด๋Š” Activity์˜ ์ˆ˜๋ช… ์ฃผ๊ธฐ์— ์ข…์†๋œ๋‹ค.

 

 

 

Flutter : ์›์†Œ์Šค ๋ฉ€ํ‹ฐ์œ ์ €๋ž€ ?

 

- Flutter๋Š” ๊ตฌ๊ธ€์—์„œ ๊ฐœ๋ฐœํ•œ ์˜คํ”ˆ์†Œ์Šค UI ์†Œํ”„ํŠธ์›จ์–ด ๊ฐœ๋ฐœ ํ‚คํŠธ(SDK)

 

- ํ•˜๋‚˜์˜ ์†Œ์Šค์ฝ”๋“œ๋กœ ๋‹ค์–‘ํ•œ ํ”Œ๋žซํผ์—์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค€๋‹ค

 

- '์›์†Œ์Šค ๋ฉ€ํ‹ฐ์œ ์ €'๋Š” Flutter์˜ ํ•ต์‹ฌ ์ฒ ํ•™ ์ค‘ ํ•˜๋‚˜

 

 

์›์†Œ์Šค ๋ฉ€ํ‹ฐ์œ ์ €

 

๋‹จ์ผ ์ฝ”๋“œ๋ฒ ์ด์Šค๋กœ iOS, Android, ์›น, ๋ฐ์Šคํฌํƒ‘ ๋“ฑ ๋‹ค์–‘ํ•œ ํ”Œ๋žซํผ์—์„œ ๋™์ž‘ํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœ

๋ณ„๋„์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ํ•„์š” ์—†์ด, ํ•˜๋‚˜์˜ ์ฝ”๋“œ๋กœ ์—ฌ๋Ÿฌ ํ™˜๊ฒฝ์— ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ๋‹ค.

๋˜ํ•œ ํ”Œ๋žซํผ ๊ฐ„์˜ UI์™€ ๋™์ž‘์˜ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

ํƒญ ๋ฐ”(Tab Bar) ๊ตฌํ˜„ํ•˜๊ธฐ

 

 

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ํ•˜๊ธฐ

 

 

 

 

 

activity_main์— RelativeLayout ๋Œ์–ด๋‹ค๋†“๊ณ , Constraint ์—ฐ๊ฒฐํ•ด์ค€๋‹ค

 

 

 

 

 

 

BottomNavigationView๋ฅผ ๊ฒ€์ƒ‰ํ•œ ํ›„, RelativeLayout ์•ˆ์— ๋„ฃ๊ณ , layout_height๋ฅผ wrap_content๋กœ ์„ค์ •ํ•œ๋‹ค.

 

 

 

 

 

BottomNavigationView ์˜ layout_alignParentBottom์„ true๋กœ ํ•ด์ค€๋‹ค.

 

 

 

 

 

BottomNavigationView ์˜ background๋ฅผ ํ•˜์–€์ƒ‰์œผ๋กœ ์„ค์ •ํ•ด์ค€๋‹ค.

 

 

 

 

 

ํด๋” ๋ชจ์–‘ ๋ฐ‘์— ์žˆ๋Š” Resource Manager๋ฅผ ํด๋ฆญํ•ด์ฃผ๊ณ , Navigation์œผ๋กœ ์ด๋™ํ•œ๋‹ค.

 

 

 

 

+ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  Navigation Resource File์„ ์„ ํƒํ•œ๋‹ค.

 

 

 

 

์ด๋ฆ„์€ my_nav๋กœ ํ•˜์˜€๋‹ค.

 

 

 

 

 

 

ํŒจํ‚ค์ง€๋กœ ๋งŒ๋“ค์–ด์„œ ํŒŒ์ผ ์ƒ์„ฑํ•ด๋„ ๋œ๋‹ค

 

 

 

 

๋‚˜ํƒ€๋‚˜๋Š” ํ™”๋ฉด์—์„œ ์•„๋ž˜์™€ ๋™์ผํ•œ ์•„์ด์ฝ˜์„ ์„ ํƒํ•˜์—ฌ Create new destination์„ ํด๋ฆญํ•œ๋‹ค

 

 

 

 

๋นˆ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์„ ํƒํ•˜์˜€๋‹ค.

 

 

 

 

 

 

๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ, 2๊ฐœ ๋” ๋งŒ๋“ค์–ด์ค€๋‹ค.

 

 

 

 

๋งˆ์šฐ์Šค๋กœ ์กฐ์ • ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

 

 

 

๋‹ค์‹œ activity_main์œผ๋กœ ์™€์„œ NavHostFragment๋ฅผ RelativeLayout์•ˆ์— ๋„ฃ์–ด์ค€๋‹ค.

 

 

 

 

 

resource์˜ layout ํด๋”์˜ fragment_first๋กœ ์ด๋™ํ•ด์„œ,

 

 

 

TextView ํด๋ฆญํ•˜๊ณ ,

 

 

 

 

 

layout_height๋ฅผ wrap_content๋กœ ์„ค์ •ํ•˜๊ณ , ํ…์ŠคํŠธ์™€ ํ…์ŠคํŠธ์‚ฌ์ด์ฆˆ๋ฅผ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค 

 

 

 

 

 

resource ํด๋”์—์„œ ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ํด๋ฆญ์œผ๋กœ, menu ํด๋”๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ 

 

 

 

 

 

๊ทธ menu ํด๋” ์•ˆ์— bottom_menu๋ผ๋Š” ๋ฉ”๋‰ด ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ค€๋‹ค

 

 

 

 

 

bottom_menu ํŒŒ์ผ์—์„œ, ์•„์ดํ…œ์„ ์„ธ๊ฐœ ๋Œ์–ด๋‹ค ๋†“๊ณ ,

 

 

 

 

 

์•„์ด๋”” ์„ค์ •๊ณผ title, icon ์„ค์ •์„ ํ•ด์ค€๋‹ค. ์•„์ด์ฝ˜์€ ์•„๋ž˜์˜ ์›น์‚ฌ์ดํŠธ์—์„œ ๋‹ค์šด๋ฐ›๋Š”๋‹ค.

 

 

 

 

home, smartdisplay , settings ์•„์ด์ฝ˜ ๋‹ค์šด๋ฐ›๊ณ  drawable ํด๋”์— ๋„ฃ๋Š”๋‹ค.

 

https://fonts.google.com/icons

 

Material Symbols and Icons - Google Fonts

Material Symbols are our newest icons consolidating over 2,500 glyphs in a single font file with a wide range of design variants.

fonts.google.com

 

 

 

 

 

 

activity_main์œผ๋กœ ์ด๋™ํ•ด์„œ, bottomNavigationView๋ฅผ ํด๋ฆญํ•œ ํ›„ menu๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์˜ค๋ฅธ์ชฝ ๋ถ€๋ถ„์„ ํด๋ฆญํ•œ๋‹ค

 

 

 

 

 

์•„๋ž˜์™€ ๊ฐ™์ด bottom menu ํƒญ ๋ฐ”๊ฐ€ ๋งŒ๋“ค์–ด์กŒ๋‹ค.

 

 

 

 

๊ธ€์ž ์•ˆ๋‚˜์˜ค๊ณ  ์•„์ด์ฝ˜๋งŒ ๋‚˜์˜ค๊ฒŒ ํ•˜๋ ค๋ฉด unlabeled ํ•˜๋ฉด ๋œ๋‹ค.

 

 

 

 

 

 

 

 

ํƒญ ๋ฐ”๋ฅผ ๋ˆ„๋ฅด๋ฉด fragment๋ฅผ ๋™์ž‘ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•

 

MainActivity.java ํด๋ž˜์Šค๋กœ ์ด๋™ํ•˜์—ฌ์„œ ์ž‘์„ฑํ•ด์ค€๋‹ค.

 

package com.~.tabbar;

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

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

import com.google.android.material.bottomnavigation.BottomNavigationItemView;
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 FirtstFragment();
        secondFragment = new SecondFragment();
        thirdFragment = new ThirdFragment();

        bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

                int itemId = menuItem.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;
        }
    }
}

 

 

 

 

 

 

ํƒญ ๋ฒ„ํŠผ ์•„์ด์ฝ˜ ์ƒ‰ ๋ฐ”๊พธ๊ธฐ 

 

์•„๋ž˜ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค.

 

https://lcw126.tistory.com/330

 

์•ˆ๋“œ๋กœ์ด๋“œ BottomNavigationView ( icon, text ์ƒ‰์ƒ ๋ณ€๊ฒฝ )

BottomNavigationView ์‚ฌ์šฉ์„ ์œ„ํ•ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค. Gradle dependencies { implementation 'com.android.support:design:29.0.0' // bottomNavigationView } activity_main.xml์— ์‚ฌ์šฉ์„ ํ•ฉ๋‹ˆ๋‹ค. ( menu๋Š” ๋ฏธ๋ฆฌ ์ด๋ฆ„์„ ์ž‘์„ฑํ–ˆ

lcw126.tistory.com