프로그래밍 정리/안드로이드

[Android - 안드로이드] Navigation Drawer or DrawerLayout

주누다 2015. 4. 17. 15:49
반응형

- DrawerLayout을 통해서 적용시킬수 있음.



drawerLyaout.openDrawer(cup_hello2); 

- 해당뷰(cup_hello2)에 layout_gravity 속성에 'start'or 'end' 속성이 적용되어있어야함.

- android:layout_gravity="start", android:layout_gravity="end"  

- start 속성은 좌측, end 속성은 우측

- left, right 속성도 마찬가지인듯...?(테스트 안 해본...)

- 양쪽의 둘다 넣을수도 있음.


- java.lang.IllegalArgumentException is not a drawer 이 옴의 에러때문에 한시간 버림...ㅠ_ㅠ



소스 첨부

ExampleNavigationDrawer.zip



소스

MainActivity.java

=============================================================================

package com.example.examplenavigationdrawer;


import android.app.Activity;

import android.os.Bundle;

import android.support.v4.view.GravityCompat;

import android.support.v4.widget.DrawerLayout;

import android.support.v4.widget.DrawerLayout.DrawerListener;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.FrameLayout;

import android.widget.TextView;


public class MainActivity extends Activity {

DrawerLayout drawer_layout;

TextView cup_hello1;

TextView cup_hello2;

TextView cup_hello3;

FrameLayout content_frame;

Button cup_btn;

DrawerListener drawListener = new DrawerListener() {

@Override

public void onDrawerStateChanged(int arg0) {

Log.i("DrawListener", "onDrawerStateChanged - arg0 : " + arg0);

}

@Override

public void onDrawerSlide(View arg0, float arg1) {

Log.i("DrawListener", "onDrawerSlide - arg0 : " + arg0);

Log.i("DrawListener", "onDrawerSlide - arg1 : " + arg1);

}

@Override

public void onDrawerOpened(View arg0) {

Log.i("DrawListener", "onDrawerOpened - arg0 : " + arg0);

}

@Override

public void onDrawerClosed(View arg0) {

Log.i("DrawListener", "onDrawerClosed - arg0 : " + arg0);

}

};


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

drawer_layout = (DrawerLayout)findViewById(R.id.drawer_layout);

drawer_layout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

drawer_layout.setDrawerListener(drawListener);

cup_hello1 = (TextView)findViewById(R.id.cup_hello1);

cup_hello2 = (TextView)findViewById(R.id.cup_hello2);

cup_hello3 = (TextView)findViewById(R.id.cup_hello3);

content_frame = (FrameLayout)findViewById(R.id.content_frame);

cup_btn = (Button)findViewById(R.id.cup_btn);

cup_btn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if(drawer_layout.isDrawerOpen(cup_hello2)){

drawer_layout.closeDrawer(cup_hello2);

}else{

drawer_layout.openDrawer(cup_hello2);

}

}

});

}

}


=============================================================================



activity_main.xml

=============================================================================

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >


    <android.support.v4.widget.DrawerLayout

        android:id="@+id/drawer_layout"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_weight="1" >


        <FrameLayout

            android:id="@+id/content_frame"

            android:layout_width="match_parent"

            android:layout_height="match_parent" >


            <TextView

                android:id="@+id/cup_hello1"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="hello_1" />


            

        </FrameLayout>


        <TextView

            android:id="@+id/cup_hello2"

            android:layout_width="240dp"

        android:layout_height="match_parent"

        android:layout_gravity="start"

        android:text="hello_2"

        android:background="#111"/>

        

        <TextView

            android:id="@+id/cup_hello3"

            android:layout_width="240dp"

        android:layout_height="match_parent"

        android:layout_gravity="end"

        android:text="hello_3"

        android:background="#111"/>


    </android.support.v4.widget.DrawerLayout>

    

    <Button

        android:id="@+id/cup_btn"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

android:layout_weight="0"

android:text="Set"/>


</LinearLayout>

=============================================================================



반응형