ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Android] Navigation Menu Customizing
    Android 2022. 3. 14. 20:57

    지난번에는 만들어진 네비게이션메뉴 템플릿을 살펴봤지만, 이번에는 직접 만들어보았다.

     

    * Linear Layout의 경우 Orientation을 반드시 설정하는 것이 좋다.

    * margin : 위아래좌우 공백 주기

    * include : layout 하나에 다른 layout 포함. 같은 화면 상에 나타낼 수 있음.

     

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <Button
                android:id="@+id/btn_open"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="열려라 참깨!!"/>
        </LinearLayout>
    
        <include layout="@layout/activity_drawer"/>
    
    </androidx.drawerlayout.widget.DrawerLayout>

     

    activity_drawer.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="240dp"
        android:id="@+id/drawer"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#989898"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/btn_close"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="메뉴 닫기"/>
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="이것은 메뉴"/>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="#0025F4"
            android:orientation="vertical">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="테스트 메뉴"/>
    
        </LinearLayout>
    
    
    
    </LinearLayout>

     

     

    MainActivity.java

    package com.example.navimenucustom;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.drawerlayout.widget.DrawerLayout;
    
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
    
        private DrawerLayout drawerLayout;
        private View drawerView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); // id 연결
            drawerView = (View) findViewById(R.id.drawer);
    
            Button btn_open = (Button) findViewById(R.id.btn_open);
            btn_open.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    drawerLayout.openDrawer(drawerView);
                } //누르면 navigation menu 열리도록
            });
    
            Button btn_close = (Button)findViewById(R.id.btn_close);
            btn_close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    drawerLayout.closeDrawers();
                }
            }); // close 눌렀을 때 닫아라
    
            drawerLayout.setDrawerListener(listner);
            drawerView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    return true; // false -> true 로 변경
                }
            });
        }
    
        // 특정 액션 시 추가 기능 넣고 싶으면 아래 이용
        DrawerLayout.DrawerListener listner = new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
    
            } // 슬라이드 시 호출
    
            @Override
            public void onDrawerOpened(@NonNull View drawerView) {
    
            } // 슬라이드 메뉴 열 때 호출
    
            @Override
            public void onDrawerClosed(@NonNull View drawerView) {
    
            } // 슬라이드 메뉴 닫을 때 호출
    
            @Override
            public void onDrawerStateChanged(int newState) {
    
            } // 상태 변화 시
        };
    }

     

    메인 화면
    네비게이션메뉴

     

Designed by Tistory.