* res > menu(폴더명 "menu"로 생성) > menu_main.xml 추가
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_refresh"
android:title="새로고침"
android:icon="@drawable/refresh"
app:showAsAction="always" />
<item
android:id="@+id/menu_search"
android:title="새로고침"
android:icon="@drawable/search"
app:showAsAction="always" />
<item
android:id="@+id/menu_settings"
android:title="새로고침"
android:icon="@drawable/setting"
app:showAsAction="always" />
</menu>
* onCreateOptionsMenu, onOptionsItemSelected 메서드 재정의
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int curId = item.getItemId();
switch (curId) {
case R.id.menu_refresh:
showToast("새로고침 메뉴 선택됨");
break;
case R.id.menu_search:
showToast("검색 메뉴 선택됨");
break;
case R.id.menu_settings:
showToast("설정 메뉴 선택됨");
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
public void showToast(String data) {
Toast.makeText(this, data, Toast.LENGTH_LONG).show();
}
}
'android' 카테고리의 다른 글
상단탭 사용법 (0) | 2020.02.02 |
---|---|
ActionBar - Home 아이콘 변경 (0) | 2020.02.02 |
Fragment 인터페이스 (0) | 2020.02.02 |
Fragment 사용법 (0) | 2020.02.01 |
SharedPreferences 사용법 (0) | 2020.02.01 |