메뉴 위쪽의 헤더 부분은 생략할 수 있고 필요에 따라 Custom View 로 대체할 수 있다. Context Menu 를 간단하게 분리해 보면 헤더의 유무와 헤더의 종류에 따라 분리할 수 있다. 아래는 각각의 경우에 따른 구현 내용이다.
공통으로 사용되는 Botton 의 Context Menu 리소스
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/MnuHi"
android:title="Hi!"
android:checkable="true"
android:checked="false"
/>
<item
android:id="@+id/MnuBye"
android:title="Bye!"
android:checkable="true"
android:checked="false"
/>
</menu>
메뉴 Header가 없는 Context Menu
메뉴 리소스로 부터 메뉴를 읽어들이기만 하면 위와 같은 심플한 Context Menu 가 출력이 된다.
public void onCreateContextMenu( ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuinfo )
{
if ( v == btnHello )
{
// menu_button.xml 에서 메뉴 불러오기
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.menu_button, menu );
}
}
시스템에서 지원하는 Header를 갖은 Context Menu
public void onCreateContextMenu( ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuinfo )
{
if ( v == btnHello )
{
// menu_button.xml 에서 메뉴 불러오기
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.menu_button, menu );
menu.setHeaderIcon( R.drawable.icon );
menu.setHeaderTitle( "Context for Button" );
}
}
Custom View 를 Header로 갖는 Context Menu
헤더에 사용된 View Layout 리소스 xml 파일 내용 (context_view_for_button.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/IVContextL"
android:src="@drawable/icon"
android:layout_gravity="top|left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/TVContext"
android:layout_gravity="center_vertical|center_horizontal"
android:text="It's Custom Header on Context"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/IVContextR"
android:src="@drawable/icon"
android:layout_gravity="top|right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
메뉴 헤더에 View 를 올리는 코드
public void onCreateContextMenu( ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuinfo )
{
if ( v == btnHello )
{
// menu_button.xml 에서 메뉴 불러오기
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.menu_button, menu );
menu.setHeaderIcon( R.drawable.icon ); // 무시됨
menu.setHeaderTitle( "Context for Button" ); // 무시됨
LayoutInflater layout = getLayoutInflater();
View v2 = layout.inflate( R.layout.context_view_for_button, null );
menu.setHeaderView( v2 );
}
}
<
Original Post : http://neodreamer-dev.tistory.com/402
No comments :
Post a Comment