본문 바로가기

플밍 is 뭔들/안드로이드

[안드로이드] DrawLayout을 이용하여 멀티윈도우 생성 예제


위의 그림과 같이 네비 버튼을 눌렀을 때 왼쪽에서 오른쪽으로 튀어나오는 레이아웃을 생성하는 예제이다.


※ 소스코드 부분
     DrawerLayout drawerLayout;
      View drawerView;
      ImageButton btn_actionBar

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list);
            
            drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
            drawerView = (View)findViewById(R.id.drawer);
            btn_actionBar = (ImageButton) findViewById(R.id.btn_actionBar);         
          
            ---그 외 소스 내용 ---
      }
      
      OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                  // TODO Auto-generated method stub
                  switch (v.getId()) {
                  case R.id.btn_actionBar:
                        drawerLayout.openDrawer(drawerView);
                        break;
                  
                  default:
                        break;
                  }
            }
      };


※ 레이아웃 부분
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
     
<LinearLayout

     -------- 화면에 나타날 레이아웃 퍼블 부분 -------------

</LinearLayout>

     -------- 왼쪽에서 튀어나올 멀티 윈도우 부분 --------------
     <LinearLayout
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/background_dark"
        android:orientation="vertical"
        android:padding="5dp" >
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Drawer" />
 
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
 
        <Button
            android:id="@+id/closedrawer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Close Drawer" />
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>