본문 바로가기
Android

안드로이드 Navigation Menu 커스텀

by Son 2022. 1. 20.

[activity_main.xml]

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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" /> /*activity_drawer을 activity_main.xm에 포함시킨다*/
 
</android.support.v4.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:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#a8a7ff" /*메뉴가 나왔을때 그 메뉴의 배경색*/
    android:id="@+id/drawer"
    android:orientation="vertical">
 
    <Button
        android:id="@+id/btn_close" /*id값*/
        android:layout_width="match_parent" /*가로길이는 부모의 길이만큼*/
        android:layout_height="wrap_content" /*버튼을 감싸는 정도 만큼의 높이*/
        android:layout_margin="10dp"  /*margin*/
        android:text="메뉴닫기"/> /*text*/
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" /*text의 위치*/
        android:text="메뉴"/>
 
    <LinearLayout
        android:layout_width="match_parent" /*부모 넓이 240dp*/
        android:layout_height="wrap_content"/*부모 높이*/
        android:layout_margin="10dp" /*margin*/
        android:background="#223ffc" /*배경색*/
        android:orientation="vertical"> /*버튼 정렬 */
 
        <TextView
            android:layout_width="match_parent"   /*부모 넓이 240dp*/
            android:layout_height="wrap_content"  /*부모 높이*/
            android:gravity="center" /*text위치*/
            android:text="테스트메뉴"/>
 
 
    </LinearLayout>
 
</LinearLayout>

------------------------------------------------------------------------------------------------------------------------------------------------------------------
[MainActivity.java]

package com.example.customnaviexample;
 
import android.support.annotation.NonNull;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
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); /*[activity_main.xml]를 drawerLayout에 넣는다*/
        drawerView = (View)findViewById(R.id.drawer); /*[activity_drawer.xml를] drawerView에 넣는다*/
 
        Button btn_open = (Button)findViewById(R.id.btn_open);
        btn_open.setOnClickListener(new View.OnClickListener() { /*버튼을 클릭했을때*/
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(drawerView); /*버튼을 클릭했을때 메뉴를 보여 주도록 함*/
            }
        });
 
        Button btn_close = (Button)findViewById(R.id.btn_close);
        btn_close.setOnClickListener(new View.OnClickListener() { /*메뉴닫기 버튼을 클릭했을때*/
            @Override
            public void onClick(View v) {
                drawerLayout.closeDrawers(); /*메뉴닫기*/
            }
        });
 
        drawerLayout.setDrawerListener(listener);
        drawerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
 
 
    }
 
 
    DrawerLayout.DrawerListener listener = new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(@NonNull View drawerView, float slideOffset) { /*onDrawerSlide 메뉴가 Slide했을 때 호출*/
 
        }
 
        @Override
        public void onDrawerOpened(@NonNull View drawerView) {  /*onDrawerOpened 메뉴가 open했을 때 호출*/
 
        }
 
        @Override
        public void onDrawerClosed(@NonNull View drawerView) { /*onDrawerClosed 메뉴가 Closed했을 때 호출*/
 
        }
 
        @Override
        public void onDrawerStateChanged(int newState) {  /*onDrawerClosed 상태가 바뀌였을 때 호출*/
  
        }
    };
 
}



 

'Android' 카테고리의 다른 글

안드로이드 (Fragment)  (0) 2022.01.26
안드로이드 카메라  (0) 2022.01.23
안드로이드 WebView  (0) 2022.01.18
안드로이드 SharedPreferences  (0) 2022.01.16
안드로이드 ListView  (0) 2022.01.13