Android

BottomNavigationView + frameLayout (fragment)

Machine_웅 2020. 4. 25. 00:47
728x90
반응형

main

public class MainActivity extends AppCompatActivity {

    private final int MAIN_FRAG = 0;  // MainActivity
    private final int Ball_FRAG = 1;  // JobUrgencyListActivity
    private final int Game_FRAG = 2;
    private final int Gym_FRAG = 3;

    public FrameLayout Frame_Main;
    public BottomNavigationView Nav_Main;

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

        // 바텀 네비게이션 설정
        Nav_Main = (BottomNavigationView) findViewById(R.id.Nav_Main);
        Frame_Main = (FrameLayout)findViewById(R.id.Frame_Main);

        Nav_Main.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.Tab_home:
                        callFragment(MAIN_FRAG);
                        return true;
                    case R.id.Tab_Ball:
                        callFragment(Ball_FRAG);
                        return true;
                    case R.id.Tab_game:
                        callFragment(Game_FRAG);
                        return true;
                    case R.id.Tab_gym:
                        callFragment(Gym_FRAG);
                        return true;
                }
                return false;
            }
        });

        // 초기 프래그먼트 설정
        callFragment(MAIN_FRAG);
    }

    private void callFragment(int frament_no){
        // 프래그먼트 사용을 위해
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (frament_no){
            case 0:
                // '프래그먼트1' 호출
                Home_Fragment fragment1 = new Home_Fragment();
                transaction.replace(R.id.Frame_Main, fragment1);
                transaction.commit();
                break;
            case 1:
                // '프래그먼트2' 호출
                Ball_Fragment fragment2 = new Ball_Fragment();
                transaction.replace(R.id.Frame_Main, fragment2);
                transaction.commit();
                break;
            case 2:
                // '프래그먼트2' 호출
                Game_Fragment fragment3 = new Game_Fragment();
                transaction.replace(R.id.Frame_Main, fragment3);
                transaction.commit();
                break;
            case 3:
                // '프래그먼트2' 호출
                Gym_Fragment fragment4 = new Gym_Fragment();
                transaction.replace(R.id.Frame_Main, fragment4);
                transaction.commit();
                break;
        }
    }

}

 

 

xml

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".Activity.MainActivity">


    <FrameLayout
        android:id="@+id/Frame_Main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/Nav_Main"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/main_tab"
        app:itemBackground="@color/main_tab"
        app:itemIconTint="@color/main_bottom_tab_color"
        app:itemTextColor="@color/main_bottom_tab_color"
        app:menu="@menu/bottom_nav_menu"
        />

</LinearLayout>
  • android:background:"@color/background” : 배경색을 설정함
  • app:itemIconTint="@color/iconText” : 아이콘의 색을 설정함
  • app:itemTextColor=”@color/iconText” : 글의 색을 설정함
  • app:menu=”@menu/bottom_navigation” : 설정한 menu의 이름을 가져와 설정을 해줌

 

 

 

 

menu  ( res - menu )

<?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/Tab_home"
        android:icon="@drawable/tab_home_on"
        android:title="홈"
        app:showAsAction="ifRoom"/>

    <item
        android:id="@+id/Tab_Ball"
        android:icon="@drawable/tab_ball_on"
        android:title="공관리"
        app:showAsAction="ifRoom"/>

    <item
        android:id="@+id/Tab_game"
        android:icon="@drawable/tab_game_on"
        android:title="대결"
        app:showAsAction="ifRoom"/>

    <item
        android:id="@+id/Tab_gym"
        android:icon="@drawable/pin"
        android:title="주변 볼링장"
        app:showAsAction="ifRoom"/>

</menu>

color (res - color)

 

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@android:color/holo_blue_dark" />
    <item android:color="@android:color/darker_gray"  />
</selector>

 

* 아이콘이 안보이는 경우 

자바 코드상에서  null 을 해주면 됨.

bottom_navigation.setItemIconTintList(null);  

728x90
반응형

'Android' 카테고리의 다른 글

fragment 현제 보고 있는 페이지 가지고 오기  (0) 2020.05.19
Fragment 백스텍 비우기  (0) 2020.04.25
인텐트 플래그  (0) 2020.04.18
list View Adapter ViewHolder  (0) 2020.04.11
안드로이드 앱내에서 언어변경  (0) 2020.04.07