365
No
class MainActivity
package com.example.choi.mystudy18;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_menu_01:
tostShow("action_menu_01");
break;
case R.id.action_menu_02:
tostShow("action_menu_02");
break;
case R.id.action_menu_03:
tostShow("action_menu_03");
break;
case R.id.menu_01:
tostShow("menu_01");
break;
case R.id.menu_02:
tostShow("menu_02");
break;
case R.id.menu_03:
tostShow("menu_03");
break;
}
return false;
}
private void tostShow(String message){
if(toast==null){
toast=Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT);
}else{
toast.setText(message);
}
toast.show();
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.choi.mystudy18.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
R.menu.main
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<group app:showAsAction="always">
<item android:title="Item1"
android:icon="@drawable/g15"
android:id="@+id/action_menu_01"
android:orderInCategory="1"
app:showAsAction="always"
/>
<item android:title="Item2"
app:showAsAction="always"
android:icon="@android:drawable/presence_online"
android:orderInCategory="2"
android:id="@+id/action_menu_02" />
<item android:title="Item3"
android:icon="@android:drawable/stat_sys_vp_phone_call_on_hold"
android:id="@+id/action_menu_03"
android:orderInCategory="3"
app:showAsAction="always" />
</group>
<group >
<item android:title="메뉴1"
android:id="@+id/menu_01" />
<item android:title="메뉴2"
android:id="@+id/menu_02" />
<item android:title="메뉴3"
android:id="@+id/menu_03" />
</group>
</menu>
댓글 ( 4)
댓글 남기기