Android > ActionBar - ¾ÆÀÌÅÛ ¼±Åýà Popup Layout º¸ÀÓ/¼û±è 󸮹æ¹ý
µî·ÏÀÏ : 2017-07-05 18:54
Á¶È¸¼ö : 53,189
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;
}