Google 제공 노트 패드 앱을 기반으로 간단한 애플리케이션을 구축 중입니다. 내 첫 번째 단계 중 하나는 가능한 경우 XML 메뉴를 사용하도록 앱을 변환하는 것입니다. noteslist.xml와menu.addIntentOptions() 사용시 메뉴 옵션 사용자 정의
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// menu initialization, use the baseline menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.noteslist, menu);
// generate any additional actions that can be performed on the
// overall list. In a normal install, there are no additional
// actions found here, but this allows other applications to extend
// our menu with their own actions.
Intent intent = new Intent(null, getIntent().getData());
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, new ComponentName(this, NotesList.class), null, intent, 0, null);
return true;
}
:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/compose_note"
android:icon="@drawable/ic_menu_compose"
android:title="@string/menu_compose"
android:alphabeticShortcut="c"
android:numericShortcut="3" />
</menu>
모든 것이 잘 작동 주요 활동, 메모 목록에서 나는 기본 '작성'메뉴 옵션을 보여 MenuInflater을 사용하고 있습니다. 이제 예제에 따라 (그리고 목록에서 항목을 선택하지 않은 상태에서 예제가 의도 옵션을 추가하려고 시도 할 때 수정 됨) 목록에 항목이 있고 그 중 하나가 선택되면 몇 가지 추가 옵션을 추가하고 싶습니다. :
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
// determine if we have any items in the list via the ListAdapter
final boolean haveItems = (getListAdapter().getCount() > 0);
// do we have items?
if (haveItems) {
// there are items, check if any are selected
//Toast.makeText(getApplicationContext(), "position: " + getSelectedItemPosition(), Toast.LENGTH_SHORT).show();
if (getSelectedItemPosition() >= 0) {
// an item is selected, add the intents for one of our list items to the menu
Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());
// build menu on the fly... always starts with the EDIT action
Intent[] specifics = new Intent[1];
specifics[0] = new Intent(Intent.ACTION_EDIT, uri);
MenuItem[] items = new MenuItem[1];
// now add additional CATEGORY_ALTERNATIVE intent-based actions, (see the manifest)
Intent intent = new Intent(null, uri);
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0, items);
// finally, add a shortcut to the edit menu item
if (items[0] != null) {
items[0].setShortcut('1', 'e');
}
}
} else {
menu.removeGroup(Menu.CATEGORY_ALTERNATIVE);
}
return true;
}
마지막으로, 데모 애플리케이션의 기본에서 변경되지 않은 참조의 AndroidManifest.xml :
1) 결과 메뉴 : 그래서
http://developer.android.com/resources/samples/NotePad/AndroidManifest.html
, 나는이 개 질문이 , 메모 편집 및 편집 항목을 선택하면 onPrepareOptionsMenu()에 의해 생성 된 제목은 기본 아이콘을 사용하고 바로 가기가 할당되지 않습니다. 안드로이드를 통해 다른 아이콘을 갖도록 intent-filter를 설정할 수 있습니다 : icon = "", 그러나 알파벳과 숫자 바로 가기를 할당하는 것과 같은 행운은 없습니다 ... 나는 이것을 지정하고 싶습니다. 이러한 메뉴 항목을 XML로 정의하고, 인 텐트 필터로 식별하여 앱에서 가져올 때 XML을 가져 와서 어떻게 든 가져 오거나 가져올 수 있습니다. 어떤 제안이나 포인터?
2)의 onCreateOptionsMenu()에서, 왜() 단지려고,이 경우 올바른 행동이 CATEGORY_ALTERNATIVE 메뉴 (추가하지 않도록 category.ALTERNATIVE로 설정 의도 필터 활동을 추가하지으로 호출 addIntentOptions입니다 onCreateOptionsMenu() 및 onPrepareOptionsMenu()에서 addIntentOptions()를 호출하여 사실상 동일한 호출이 발생하는 방식에 대해 서로 머리를 맞 춥니 다.
@pmmenneg
는 "추가 항목의 임의의 수의 UI를 관리하는 ... 도전이기 때문에 ... 우리는이 방법에서 멀리 이동" addIntentOptions()'는 해당 샘플 코드의 특정 비트 외부에서 일반적으로 사용되지 않습니다. – CommonsWare
@CommonsWare : 아, 감사합니다. 인 텐트와 관련된 메뉴 항목을 추가하는 가장 좋은 방법은 무엇입니까? –
@pmmenneg : 'Intents'와 관련된 메뉴 항목을 추가하지 않는 것이 좋습니다. Google은 제 3자가 다른 애플 리케이션의 메뉴에 나타나기 위해 임의의 '인 텐트'를 추가 했으므로 메뉴 유출로 이어질 것이라는 결론을 내 렸습니다. – CommonsWare