์๋๋ก์ด๋ ์คํ๋์ค 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