본문 바로가기

개발/모바일

안드로이드 스튜디오 Lec4 내비게이션 메뉴 바 만들기 (To Make Menu)

반응형

강의링크 : https://youtu.be/7L17qFN2I6k


- 햄버거 버튼을 누르면 drawer 오픈

- 리스트 클릭 시 해당 화면으로 전환



1.

app/res/layout폴더에서 first_layout.xml생성 & 텍스트를 "첫 번째 화면"으로 변경


2.

복사&붙여넣기로 같은 폴더에 second_layout.xml생성 & 텍스트를 "두 번째 화면"으로 변경


3. 

app/java/com.example~~ 폴더(MainActivity파일이 있는 폴더)에 FirstLayout java파일 생성

public class FirstLayout extends Fragment

FirstLayout 클래스에 Fragment를 extends.


 FirstLayout

 package com.example.han.tutorial4;


import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* Created by HAN on 2018. 1. 7..
*/

// extends Fragment
public class FirstLayout extends Fragment {
// command + n 누르면 override methods 활용 가능
View v;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.first_layout, container, false);
return v;
}
}


같은 방법으로 SecondLayout생성



4.

app/res/menu/activity_main_drawer.xml 파일에서 리스트에 나올 텍스트와 id연결 수정

activity_main_drawer.xml 

 <?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

<group android:checkableBehavior="single">
<item
android:id="@+id/nav_first_layout"
android:icon="@drawable/ic_menu_camera"
android:title="첫 번째 화면" />
<item
android:id="@+id/nav_second_layout"
android:icon="@drawable/ic_menu_gallery"
android:title="두 번째 화면 " />

</group>

<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="Share" />
<item
android:id="@+id/nav_send"
android:icon="@drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>

</menu>


5. 


R.id.content_main으로 접근하기 위해 app/res/layout/content_main.xml에 아래의 코드 추가

  android:id="@+id/content_main"


content_main.xml

 <?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.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:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.han.tutorial4.MainActivity"
tools:showIn="@layout/app_bar_main">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>



6.


 MainActivity

 package com.example.han.tutorial4;


import android.app.FragmentManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

// ********* 이 부분에 코드를 추가함!
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager manager = getFragmentManager();
if (id == R.id.nav_first_layout) {
manager.beginTransaction().replace(R.id.content_main, new FirstLayout()).commit();
} else if (id == R.id.nav_second_layout) {
manager.beginTransaction().replace(R.id.content_main, new SecondLayout()).commit();
} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_send) {

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}


반응형