주소관리 앱개발 ver.1 (1일차)
- 새프로젝트 생성
- Target Android Devices : Phone and Table(API 15 이상 선택)
- Add and Activity to Mobile : Navigation Drawer Activity
- /layout/content_main.xml 수정
12345678910111213141516171819202122<?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:ads="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="@string/appbar_scrolling_view_behavior"tools:context="kr.co.moak.proj001address.MainActivity"tools:showIn="@layout/app_bar_main"><FrameLayoutandroid:id="@+id/container"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintBottom_toTopOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"></FrameLayout></android.support.constraint.ConstraintLayout>- TextView를 삭제하고 FrameLayout을 추가
- FrameLayout의 id를 container로 지정
- MainActivity.java : ft.replace(R.id.container, fragRegister);
- layout_width : match_parent
- layout_height : match_parent
- /layout/frag_register 생성
- File name : frag_register
- Root element : ScrollView 선택
- ScrollView에 LinearLayout(vertical)추가
123456789<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent" android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"></LinearLayout></ScrollView> - LinearLayout에 Button, EditText 등을 추가하여 화면 구성
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent" android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="@string/name" /><EditTextandroid:id="@+id/txtName"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="3"android:ems="10"android:inputType="textPersonName" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="@string/telephone" /><Spinnerandroid:id="@+id/spnTel1"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1.2"android:ems="10"android:entries="@array/member_spin_tel1" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="0.2"android:text="-" /><EditTextandroid:id="@+id/txtTel2"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="0.7"android:ems="10"android:gravity="center_horizontal"android:hint="@string/member_hint_tel2"android:inputType="numberDecimal" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="0.2"android:text="-" /><EditTextandroid:id="@+id/txtTel3"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="0.7"android:ems="10"android:gravity="center_horizontal"android:hint="@string/member_hint_tel3"android:inputType="numberDecimal" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><Buttonandroid:id="@+id/btnSave"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:text="@string/save" /><Buttonandroid:id="@+id/btnCancel"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:text="@string/cancel" /></LinearLayout></LinearLayout></ScrollView>
- /src/FragRegister.java 생성
12345678910public class FragRegister extends android.support.v4.app.Fragment {@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.frag_register, container, false);return view;}}- 꼭 android.support.v4.app.Fragment에서 상속 받아야 함
- onCre을 입력하며 나타나는 메뉴에서 onCreateView를 찾아 클릭한 후 완성하면 됨
- /layout/frag_list.xml 생성
123456789101112<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/textView2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="TextView" /></LinearLayout> - /src/FragList.java 생성
123456789public class FragList extends android.support.v4.app.Fragment {@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.frag_list, container, false);return view;}} - /menu/activity_main_drawer.xml 수정
123456789101112131415161718192021222324252627282930313233343536<?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"><itemandroid:id="@+id/nav_register"android:title="Register" /><itemandroid:id="@+id/nav_list"android:title="List" /><itemandroid:id="@+id/nav_slideshow"android:icon="@drawable/ic_menu_slideshow"android:title="Slideshow" /><itemandroid:id="@+id/nav_manage"android:icon="@drawable/ic_menu_manage"android:title="Tools" /></group><item android:title="Communicate"><menu><itemandroid:id="@+id/nav_share"android:icon="@drawable/ic_menu_share"android:title="Share" /><itemandroid:id="@+id/nav_send"android:icon="@drawable/ic_menu_send"android:title="Send" /></menu></item></menu> - MainActivity.java의 onNavigationItemSelected() 메서드 수정
123456789101112131415161718192021222324@SuppressWarnings("StatementWithEmptyBody")@Overridepublic boolean onNavigationItemSelected(MenuItem item) {// Handle navigation view item clicks here.int id = item.getItemId();FragmentTransaction ft = getSupportFragmentManager().beginTransaction();;if (id == R.id.nav_register) {Fragment fragRegister = new FragRegister();ft.replace(R.id.container, fragRegister);ft.commit();} else if (id == R.id.nav_list) {Fragment fragList = new FragList();ft.replace(R.id.container, fragList);ft.commit();} else if (id == R.id.nav_camera) {}DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);drawer.closeDrawer(GravityCompat.START);return true;}