안드로이드

 

 

 

 

 

 

 

 

class MainActivity

 


    @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;
    }

 

activit_main_drawer.xml

 


    <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>

 

 

first_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="163dp"
        android:text="첫 번째 화면"
        android:textSize="30sp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="99dp"
        android:layout_marginEnd="99dp" />
</RelativeLayout>

 

 

second_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="96dp"
        android:layout_marginRight="96dp"
        android:layout_marginTop="163dp"
        android:text="두 번째 화면"
        android:textSize="30sp" />
</RelativeLayout>

 

 

content_main.xml 아이디값 입력

   android:id="@+id/content_main"

 

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="kr.co.dothome.braverokmc.tutorial4.MainActivity"
    tools:showIn="@layout/app_bar_main"
    android:id="@+id/content_main"
    >

 

 

class FirstLayout

package kr.co.dothome.braverokmc.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 choi on 2017-04-24.
 */

public class FirstLayout extends Fragment {

    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        view=inflater.inflate(R.layout.first_layout, container,false);

        return view;
    }
}

 

class SecondLayout

package kr.co.dothome.braverokmc.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 choi on 2017-04-24.
 */

public class SecondLayout extends Fragment {

    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        view=inflater.inflate(R.layout.second_layout, container,false);

        return view;
    }
}

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

나는 계속 배우면서 나는 갖추어 간다. 언젠가는 나에게도 기회가 올 것이다. -링컨

댓글 ( 4)

댓글 남기기

작성