반응형
/app/res/menu/menu_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/tab1"
android:enabled="true"
android:icon="@android:drawable/ic_dialog_email"
android:title="이메일"
app:showAsAction="ifRoom" />
<item
android:id="@+id/tab2"
android:icon="@android:drawable/ic_dialog_info"
android:title="정보"
app:showAsAction="ifRoom" />
<item
android:id="@+id/tab3"
android:icon="@android:drawable/ic_dialog_map"
android:title="위치"
app:showAsAction="ifRoom" />
</menu>
@android:drawable은 안드로이드가 기본 제공하는 이미지를 참조하도록 한다
/drawable/item_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#51032d" android:state_checked="true" />
<item android:color="#CFD8DC" />
</selector>
activity_main
<?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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
app:itemBackground="?colorPrimary"
app:itemIconTint="@drawable/item_color"
app:itemTextColor="@drawable/item_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/menu_bottom" />
</androidx.constraintlayout.widget.ConstraintLayout>
하단 탭을 보여주는 위젯 BottomNavigationView가 하단에 표시될 수 있도록 해야한다
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
각 탭의 배경색, 아이콘 색상, 텍스트 색상을 정한다
app:itemBackground="?colorPrimary"
app:itemIconTint="@drawable/item_color"
app:itemTextColor="@drawable/item_color"
menu 속성의 값으로 menu_bottom.xml 파일의 내용이 탭으로 보이게 한다
app:menu="@menu/menu_bottom"
상단 탭 만들기에서 만들었던 프래그먼트 자바 파일들과 레이아웃 파일들을 복사해온다
https://junyoung-developer.tistory.com/153
BottomNavigationView의 리스너 setOnNavigationItemSelectedListener는 폐기되었다
MainActivity
package com.example.a34_bottomnavigation;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.navigation.NavigationBarView;
public class MainActivity extends AppCompatActivity {
Fragment1 fragment1;
Fragment2 fragment2;
Fragment3 fragment3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment1 = new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment1).commit();
// BottomNavigationView의 리스너 setOnNavigationItemSelectedListener는 폐기되었다
// BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
// bottomNavigationView.setOnNavigationItemSelectedListener();
NavigationBarView navigationBarView = findViewById(R.id.bottom_navigation);
navigationBarView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.tab1:
Toast.makeText(getApplicationContext(), "첫 번째 탭 선택됨", Toast.LENGTH_SHORT).show();
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment1).commit();
return true;
case R.id.tab2:
Toast.makeText(getApplicationContext(), "두 번째 탭 선택됨", Toast.LENGTH_SHORT).show();
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment2).commit();
return true;
case R.id.tab3:
Toast.makeText(getApplicationContext(), "세 번째 탭 선택됨", Toast.LENGTH_SHORT).show();
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment3).commit();
return true;
}
return false;
}
});
}
}
반응형
'안드로이드' 카테고리의 다른 글
바로가기 메뉴, Navigation Drawer (0) | 2021.10.21 |
---|---|
ViewPager2로 여러 프래그먼트 간 슬라이드 (0) | 2021.10.21 |
상단 탭 만들기 (0) | 2021.10.20 |
[에러] Installation failed due to: ''cmd package install-create .... (0) | 2021.10.20 |
액션바에 검색창 만들기 (0) | 2021.10.20 |