Android > ActionBar - 아이템 선택시 Popup Layout 보임/숨김 처리방법
등록일 : 2017-07-05 18:54
조회수 : 93,852
ActionBar 에서 아이템을 선택할 시 Layout 을 보임/숨김 처리하는 방법에 대한 예제를 만들어 보도록 하겠습니다.
menu 폴더의 [menu.xml] 를 만듭니다.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:showAsAction="ifRoom|withText"
android:id="@+id/item1"
android:icon="@android:drawable/ic_menu_add"
android:title="Add"
/>
<item
android:id="@+id/item2"
android:showAsAction="always"
android:title="Action item with icon"
android:icon="@android:drawable/ic_menu_search"
/>
<item
android:title="Normal item"
android:id="@+id/item3"
android:icon="@drawable/btn_notice"
android:showAsAction="always"/>
</menu>
btn_notice.png 파일은 res 폴더 밑에 [drawable] 폴더를 만들고 아이콘을 폭사해 넣습니다. 이 아이콘을 누를경우 layout을 보임/숨김 처리할 예정 입니다.
이제 보임/숨김 처리 할 layout 하나를 만듭니다.
popup_background.9.png 파일은 res 폴더 밑에 [drawable] 폴더를 만들고 아이콘을 폭사해 넣습니다. 배경파일로 사용할 파일입니다.
[value] 폴더 strings.xml 파일에 아래 내용을 추가해 줍니다.
<string name="notify">알림창 입니다.</string>
layout 폴더의 [layout_noti.xml] 를 아래처럼 디자인 합니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/actionbar_notify_pannel"
style="@style/AppBaseTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginRight="20dp"
android:background="@drawable/popup_background"
android:orientation="vertical"
android:paddingTop="10dp"
android:visibility="gone" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@string/notify"
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="테스트"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@string/notify"
android:textSize="20sp"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/btn_notice" />
</LinearLayout>
팝업을 띄울 디자인까지 모두 준비되었습니다.
item3 아이콘을 클릭할 때 보임/숨김 처리하는 방법을 알아보겠습니다.
ID는 actionbar_notify_pannel 입니다.
java 파일 내용입니다. 추가한 내용만 적어 놓도록 하겠습니다.
ActionBar actionBar; //ActionBar 참조변수
boolean showNotiPannel;
LinearLayout notiPannel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar abar = getActionBar();
abar.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS );
setContentView(R.layout.layout_noti); //팝업할 xml을 view에 추가합니다.
notiPannel = (LinearLayout) findViewById(R.id.actionbar_notify_pannel); //LinearLayout 을 읽어 드립니다.
showNotiPannel = false;
}
//아래는 Item을 선택했을 경우 처리하는 로직 입니다. //Item3 부분만 확인하면 될것 같습니다.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
String text = null;
switch(item.getItemId()){
case R.id.item1:
text = "Action item, with text, displayed if room exists";
break;
case R.id.item2:
text = "Action item, icon only, always displayed";
break;
case R.id.item3:
text = "Normal menu item";
if ( showNotiPannel ){
notiPannel.setVisibility(View.GONE);
showNotiPannel = false;
}else{
notiPannel.setVisibility(View.VISIBLE);
showNotiPannel = true;
}
break;
default:
return false;
}
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
return true;
}