SORU
18 ŞUBAT 2012, CUMARTESİ


Nasıl alt menü öğeleri eklemek için kod ActionBar eylem?

Xml) ActionBar benim eylem için bir alt menü öğeleri ekleyebilirsiniz.

enter image description here

main_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@ id/menu_new_form"
          android:icon="@drawable/ic_new_form"
          android:title="@string/menu_new_form"
          android:showAsAction="ifRoom|withText">
        <menu>
            <item android:id="@ id/form1"
                  android:icon="@drawable/attachment"
                  android:title="Form 1"
                  android:onClick="onSort" />
            <item android:id="@ id/form2"
                  android:icon="@drawable/attachment"
                  android:title="Form 2"
                  android:onClick="onSort" />
        </menu>
    </item>
</menu>

Ama nasıl Java kodu ile bu alt öğeleri ekleyebilirim? İşe yaramıyor gibi, aşağıda, alt öğeleri almak için eklenen yanlış eylem (ve de drawable değil gösterilen), çok sağ düğme değil, benim 'Yeni Formu' düğmesi:

enter image description here

main_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@ id/menu_new_form"
          android:icon="@drawable/ic_new_form"
          android:title="@string/menu_new_form"
          android:showAsAction="ifRoom|withText">
    </item>
</menu>

Java Kod:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);

    Log.d("MainMenu", ",menu title0: "   menu.getItem(0).getTitle()); 
    // returns "New Form"

    menu.addSubMenu(0, Menu.NONE, 1, "Form 1").setIcon(R.drawable.attachment);
    menu.addSubMenu(0, Menu.NONE, 2, "Form 2").setIcon(R.drawable.attachment);
    return true;
}

Bu elde etmek için bir yol, Java Kod yerine XML ile alt menü öğeleri eklemeolmadanPopupMenu (http://developer.android.com/guide/topics/ui/menus.html#PopupMenu)bir? kullanma

Güncelleme (Çözümü):

Benim son kod adamp cevabı aşağıdaki alt menü dinamik olarak doldurmak ile sona erdi, parçacığı:

// menu options
private static final int MENU_PREFERENCES = Menu.FIRST;
private static final int MENU_LOGOUT = 2;

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    menu.add(0, MENU_PREFERENCES, 0, getString(R.string.general_preferences)).setIcon(
            android.R.drawable.ic_menu_preferences);

    // load all available form templates
    Cursor c = managedQuery(FormsProviderAPI.FormsColumns.CONTENT_URI, null, null, null, null);
    try {
        int ixDisplayName = c.getColumnIndex(FormsProviderAPI.FormsColumns.DISPLAY_NAME);
        int ixId = c.getColumnIndex(FormsProviderAPI.FormsColumns._ID);
        int cnt = 0;
        while (c.moveToNext()) {
            cnt  ;
            Log.d("ID: ", "ID: "  c.getInt(ixId));  // misusing the group id for the form id
            menu.getItem(1).getSubMenu().addSubMenu(c.getInt(ixId), Menu.NONE, cnt, c.getString(ixDisplayName)).setIcon(R.drawable.attachment_dark);
        }
    } catch (Exception e) {
        Log.e(TAG, "Error init form templates list.", e);
    }

    return true;
}

CEVAP
18 ŞUBAT 2012, CUMARTESİ


Evet, var.

addSubMenu yöntemi SubMenu nesne döndürür. SubMenu bir de üst menü yerine, Alt menü öğeleri eklemek için add diyebilirsiniz ** 19. Kodunuzu yukarıdaki FORMU 1 için iki farklı alt menüler ve Form 2 yerine tek bir Form içinde iki alt menü öğeleri oluşturuyor.

Örnek:

SubMenu submenu = menu.addSubMenu(0, Menu.NONE, 1, "New Form").setIcon(R.drawable.ic_new_form);
submenu.add("Form 1").setIcon(R.drawable.attachment);

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • DONFANTASTICKYPESS

    DONFANTASTIC

    1 Temmuz 2007
  • Need for Speed

    Need for Spe

    8 ŞUBAT 2006
  • TV nEW

    TV nEW

    25 AĞUSTOS 2012