Android 학습 08 --- Android 의 기본 컨트롤 (하) (6)메뉴: Menu 와 스텔스 서랍 구성 요소: SlidingDrawer


메뉴
1. 옵션 메뉴, 프로그램 을 통 해 생 성
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="  Menu       " />

</LinearLayout>
 
 
Menu01_Activity.java
package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class Menu01_Activity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, Menu.FIRST + 1, 5, "  ").setIcon(
				android.R.drawable.ic_menu_delete);
		menu.add(Menu.NONE, Menu.FIRST + 2, 2, "  ").setIcon(
				android.R.drawable.ic_menu_save);
		menu.add(Menu.NONE, Menu.FIRST + 3, 6, "  ").setIcon(
				android.R.drawable.ic_menu_help);
		menu.add(Menu.NONE, Menu.FIRST + 4, 1, "  ").setIcon(
				android.R.drawable.ic_menu_add);
		menu.add(Menu.NONE, Menu.FIRST + 5, 4, "  ").setIcon(
				android.R.drawable.ic_menu_info_details);
		menu.add(Menu.NONE, Menu.FIRST + 6, 7, "  ").setIcon(
				android.R.drawable.ic_menu_send);
		menu.add(Menu.NONE, Menu.FIRST + 7, 3, "  ").setIcon(
				android.R.drawable.ic_menu_edit);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) { //        ID
		case Menu.FIRST + 1:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 2:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 3:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 4:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 5:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 6:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 7:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		}
		return false;
	}

	@Override
	public void onOptionsMenuClosed(Menu menu) {
		Toast.makeText(this, "       ", Toast.LENGTH_LONG).show();
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		Toast.makeText(this,
				"     (onCreateOptionsMenu()          ,                 。)",
				Toast.LENGTH_LONG).show();
		return true;
	}

}
 
 
2. 설정 파일 을 통 해 메뉴 생 성
Menu02_Activity.java
package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class Menu02_Activity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.getMenuInflater().inflate(R.menu.mymenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) { //        ID
		case Menu.FIRST + 1:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 2:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 3:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 4:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 5:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 6:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 7:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		}
		return false;
	}

	@Override
	public void onOptionsMenuClosed(Menu menu) {
		Toast.makeText(this, "       ", Toast.LENGTH_LONG).show();
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		Toast.makeText(this,
				"     (onCreateOptionsMenu()          ,                 。)",
				Toast.LENGTH_LONG).show();
		return true;
	}
}
 
 
Res/menu/ mymenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:id="@+id/item01" android:title="  "
		android:icon="@android:drawable/ic_menu_add" />
	<item android:id="@+id/item02" android:title="  "
		android:icon="@android:drawable/ic_menu_save" />
	<item android:id="@+id/item03" android:title="  "
		android:icon="@android:drawable/ic_menu_edit" />
	<item android:id="@+id/item04" android:title="  "
		android:icon="@android:drawable/ic_menu_info_details" />
	<item android:id="@+id/item05" android:title="  "
		android:icon="@android:drawable/ic_menu_delete" />
	<item android:id="@+id/item06" android:title="  "
		android:icon="@android:drawable/ic_menu_send" />
	<item android:id="@+id/item07" android:title="  "
		android:icon="@android:drawable/ic_menu_help" />
</menu>
 
 
3. 프로그램 구현 컨 텍스트 메뉴
Menu03_Activity.java
package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Menu03_Activity extends Activity {
	private String data[] = new String[] { "hello", "www.iteye.com", "Android" }; //   ListView    
	private ListView listView = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.listView = new ListView(this);
		this.listView.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_expandable_list_item_1, this.data));
		super.setContentView(this.listView);
		super.registerForContextMenu(this.listView);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case Menu.FIRST + 1:
			Toast.makeText(this, "     “     ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 2:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 3:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 4:
			Toast.makeText(this, "     “   ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 5:
			Toast.makeText(this, "     “  ” ", Toast.LENGTH_LONG).show();
			break;
		}
		return false;
	}

	@Override
	public void onContextMenuClosed(Menu menu) {
		Toast.makeText(this, "        ", Toast.LENGTH_LONG).show();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("    ");
		menu.add(Menu.NONE, Menu.FIRST + 1, 1, "     ");
		menu.add(Menu.NONE, Menu.FIRST + 2, 2, "    ");
		menu.add(Menu.NONE, Menu.FIRST + 3, 3, "    ");
		menu.add(Menu.NONE, Menu.FIRST + 4, 4, "   ");
		menu.add(Menu.NONE, Menu.FIRST + 5, 5, "  ");
	}
}
 
 
4. 컨 텍스트 메뉴 설정
Menu04_Activity.java
package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Menu04_Activity extends Activity {
	private String data[] = new String[] { "hello", "www.iteye.com", "Android" }; //   ListView    
	private ListView listView = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.listView = new ListView(this);
		this.listView.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_expandable_list_item_1, this.data));
		super.setContentView(this.listView);
		super.registerForContextMenu(this.listView);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case Menu.FIRST + 1:
			Toast.makeText(this, "     “     ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 2:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 3:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 4:
			Toast.makeText(this, "     “   ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 5:
			Toast.makeText(this, "     “  ” ", Toast.LENGTH_LONG).show();
			break;
		}
		return false;
	}

	@Override
	public void onContextMenuClosed(Menu menu) {
		Toast.makeText(this, "        ", Toast.LENGTH_LONG).show();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("    ");
		super.getMenuInflater().inflate(R.menu.mymenu, menu);
	}
}

 
Res/ menu/ mymenu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:id="@+id/item01" android:title="     " />
	<item android:id="@+id/item02" android:title="    " />
	<item android:id="@+id/item03" android:title="    " />
	<item android:id="@+id/item04" android:title="   " />
	<item android:id="@+id/item05" android:title="  " />
</menu>

 
5. 하위 메뉴 기본 구현
Menu05_Activity.java
package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Menu05_Activity extends Activity {
	private String data[] = new String[] { "hello", "www.iteye.com", "Android" }; //   ListView    
	private ListView listView = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.listView = new ListView(this);
		this.listView.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_expandable_list_item_1, this.data));
		super.setContentView(this.listView);
		super.registerForContextMenu(this.listView);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		SubMenu fileMenu = menu.addSubMenu("  ");
		SubMenu editMenu = menu.addSubMenu("  ");
		fileMenu.add(Menu.NONE, Menu.FIRST + 1, 1, "  ");
		fileMenu.add(Menu.NONE, Menu.FIRST + 2, 2, "  ");
		fileMenu.add(Menu.NONE, Menu.FIRST + 3, 3, "  ");
		editMenu.add(Menu.NONE, Menu.FIRST + 4, 4, "  ");
		editMenu.add(Menu.NONE, Menu.FIRST + 5, 5, "  ");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) { //        ID
		case Menu.FIRST + 1:
			Toast.makeText(this, "     “     ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 2:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 3:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 4:
			Toast.makeText(this, "     “   ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 5:
			Toast.makeText(this, "     “  ” ", Toast.LENGTH_LONG).show();
			break;
		}
		return false;
	}

}

 
6. 설정 을 통 해 하위 메뉴 구현
Res/menu/ editmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:id="@+id/item04" android:title="  " />
	<item android:id="@+id/item05" android:title="  " />
</menu>

 
filemenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:id="@+id/item01" android:title="  " />
	<item android:id="@+id/item02" android:title="  " />
	<item android:id="@+id/item03" android:title="  " />
</menu>

 
Menu06_Activity.java
package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Menu06_Activity extends Activity {
	private String data[] = new String[] { "hello", "www.iteye.com", "Android" }; //   ListView    
	private ListView listView = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.listView = new ListView(this);
		this.listView.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_expandable_list_item_1, this.data));
		super.setContentView(this.listView);
		super.registerForContextMenu(this.listView);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		SubMenu fileMenu = menu.addSubMenu("  ");
		SubMenu editMenu = menu.addSubMenu("  ");
		super.getMenuInflater().inflate(R.menu.filemenu, fileMenu);
		super.getMenuInflater().inflate(R.menu.editmenu, editMenu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) { //        ID
		case Menu.FIRST + 1:
			Toast.makeText(this, "     “     ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 2:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 3:
			Toast.makeText(this, "     “    ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 4:
			Toast.makeText(this, "     “   ” ", Toast.LENGTH_LONG).show();
			break;
		case Menu.FIRST + 5:
			Toast.makeText(this, "     “  ” ", Toast.LENGTH_LONG).show();
			break;
		}
		return false;
	}

}

 
2. 암시 적 서랍 구성 요소: SlidingDrawer
SlidingDrawerActivity.java
package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.SlidingDrawer.OnDrawerScrollListener;
import android.widget.Toast;

public class SlidingDrawerActivity extends Activity {
	private String data[] = new String[] { "hello", "www.iteye.com", "Android" }; //   ListView    
	private ListView listView = null;
	private SlidingDrawer slidingDrawer = null;
	private ImageView handle = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		LinearLayout layout = (LinearLayout) super.findViewById(R.id.content); //      ListView
		this.listView = new ListView(this);
		this.listView.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_expandable_list_item_1, this.data));
		layout.addView(this.listView);

		this.slidingDrawer = (SlidingDrawer) super
				.findViewById(R.id.slidingdrawer);
		this.handle = (ImageView) super.findViewById(R.id.handle);

		this.slidingDrawer
				.setOnDrawerOpenListener(new OnDrawerOpenListenerImpl());
		this.slidingDrawer
				.setOnDrawerCloseListener(new OnDrawerCloseListenerImpl());
		this.slidingDrawer
				.setOnDrawerScrollListener(new OnDrawerScrollListenerImpl());
	}

	private class OnDrawerOpenListenerImpl implements OnDrawerOpenListener {

		@Override
		public void onDrawerOpened() {
			SlidingDrawerActivity.this.handle
					.setImageResource(R.drawable.ico_right);
		}

	}

	private class OnDrawerCloseListenerImpl implements OnDrawerCloseListener {

		@Override
		public void onDrawerClosed() {
			SlidingDrawerActivity.this.handle
					.setImageResource(R.drawable.ico_left);
		}

	}

	private class OnDrawerScrollListenerImpl implements OnDrawerScrollListener {

		@Override
		public void onScrollEnded() {
			Toast.makeText(SlidingDrawerActivity.this, "      。",
					Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onScrollStarted() {
			Toast.makeText(SlidingDrawerActivity.this, "      。",
					Toast.LENGTH_SHORT).show();
		}

	}
}

 
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <SlidingDrawer
        android:id="@+id/slidingdrawer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:content="@+id/content"
        android:handle="@+id/handle"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/handle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ico_left" />

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
        </LinearLayout>
    </SlidingDrawer>

</LinearLayout>
 

좋은 웹페이지 즐겨찾기