코딩을 하다보면 일반 레이아웃 객체는 쉽게 선언 할 수 있는데 안드로이드 툴바 메뉴객체는 어떻게 선언해야하지? 하면서 모

를 때가 있습니다. 이럴때는 이렇게 하시면 됩니다.


add_menu.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/add_all_check"
android:title="@string/choice_all"
android:checkable="true"
android:checked="false"
android:orderInCategory="101"
app:showAsAction="ifRoom"
/>

<item
android:id="@+id/add_check_num"
android:title=""
android:orderInCategory="102"
app:showAsAction="ifRoom"
/>
</menu>

-----------------------------------------------------------------------------------------------------------------

/**전체선택 메뉴*/
private MenuItem mAddAllCheckMenu;

/**선택 개수 메뉴*/
private MenuItem mAddCheckNumMenu;


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add_menu, menu);
mAddAllCheckMenu = menu.findItem(R.id.add_all_check);
mAddCheckNumMenu = menu.findItem(R.id.add_check_num);
mAddCheckNumMenu.setTitle(String.format(getString(R.string.choice_check_num), 0));
return true;
}


    @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.
int id = item.getItemId();
Intent intent = null;
if (id == R.id.add_all_check) {
if (!mAddAllCheckMenu.isChecked()) {
mAddAllCheckMenu.setChecked(true);
mAddAllCheckMenu.setTitle(getString(R.string.choice_all));
setAllChecked(true);
mAddCheckNumMenu.setTitle(String.format(getString(R.string.choice_check_num), getCheckedCount()));
} else {
mAddAllCheckMenu.setChecked(false);
mAddAllCheckMenu.setTitle(getString(R.string.choice_all));
setAllChecked(false);
mAddCheckNumMenu.setTitle(String.format(getString(R.string.choice_check_num), 0));
}
} else if (id == R.id.add_check_num) {
return false;

}


return super.onOptionsItemSelected(item);
}
}


728x90
반응형

+ Recent posts