- activity_main.xml
<FrameLayout
android:id="@+id/frame_fragments"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
app:itemIconTint="@color/bottom_navigation_color"
app:itemTextColor="@color/bottom_navigation_color"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/item_navigation" />
- color/bottom_navigation_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#7A59C5" android:state_checked="true" />
<item android:color="#9F9F9F" />
</selector>
- menu/item_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_home"
android:enabled="true"
android:icon="@drawable/ic_home"
android:title="Home" />
<item
android:id="@+id/action_search"
android:enabled="true"
android:icon="@drawable/ic_search"
android:title="Search"/>
<item
android:id="@+id/action_wish"
android:enabled="true"
android:icon="@drawable/ic_heart"
android:title="Wish"/>
<item
android:id="@+id/action_star"
android:enabled="true"
android:icon="@drawable/ic_star"
android:title="Star"/>
<item
android:id="@+id/action_mypage"
android:enabled="true"
android:icon="@drawable/ic_user"
android:title="MyPage"/>
</menu>
- HomeFragment
class HomeFragment : Fragment() {
companion object {
fun newInstance() : HomeFragment = HomeFragment()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onAttach(context: Context) {
super.onAttach(context)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
//반환형 : View
}
}
- MainActivity
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener {
//멤버변수
private lateinit var homeFragment: HomeFragment
private lateinit var searchFragment: SearchFragment
private lateinit var wishFragment: WishFragment
private lateinit var starFragment: StarFragment
private lateinit var mypageFragment: MypageFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
bottom_navigation.setOnNavigationItemSelectedListener(this)
homeFragment = HomeFragment.newInstance()
supportFragmentManager.beginTransaction().add(R.id.frame_fragments, homeFragment).commit()
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.menu_home ->{
homeFragment = HomeFragment.newInstance()
supportFragmentManager.beginTransaction().replace(R.id.frame_fragments, homeFragment).commit()
}
R.id.menu_search -> {
searchFragment = SearchFragment.newInstance()
supportFragmentManager.beginTransaction().replace(R.id.frame_fragments, searchFragment).commit()
}
R.id.menu_wish -> {
wishFragment = WishFragment.newInstance()
supportFragmentManager.beginTransaction().replace(R.id.frame_fragments, wishFragment).commit()
}
R.id.menu_star -> {
starFragment = StarFragment.newInstance()
supportFragmentManager.beginTransaction().replace(R.id.frame_fragments, starFragment).commit()
}
R.id.menu_mypage -> {
mypageFragment = MypageFragment.newInstance()
supportFragmentManager.beginTransaction().replace(R.id.frame_fragments, mypageFragment).commit()
}
}
return true
}
}
'안드로이드 > 앱 개발' 카테고리의 다른 글
statusBar 색상, 텍스트 색상 변경 (0) | 2020.12.13 |
---|---|
BottomNavigationView + TabLayout + ViewPager2 모두 이용해서 화면 만들기 (Kotlin) (0) | 2020.12.12 |
TabLayout (0) | 2020.12.12 |
UI 디자인 참고자료 (0) | 2020.12.11 |
Glide (0) | 2020.12.04 |