0

ナビゲーションメニュー項目があります。現在、onCreateOptionsMenu で手動で作成しています。

public boolean onCreateOptionsMenu(Menu menu) {
    mLocations = getResources().getStringArray(R.array.locations);
    Context context = getSupportActionBar().getThemedContext();
    ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(
    context, R.array.locations, R.layout.sherlock_spinner_item);
    list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    getSupportActionBar().setListNavigationCallbacks(list, this);
}

XML で定義する方法はありますか?

4

1 に答える 1

0

2 つのことを行う必要があります。ここにmain.xmlがあります:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

<item
    android:id="@+id/exit"
    android:orderInCategory="200"
    android:showAsAction="never"
    android:title="@string/exit"/>

</menu>

次に、メイン アクティビティで XML をインフレートします。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getSupportMenuInflater().inflate(R.menu.main, menu);
    return true;
}

フラグメントを使用している場合は、次のようになります。

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the menu; this adds items to the action bar if it is present.
    inflater.inflate(R.menu.fragment, menu);
}

もちろん、それに対応するfragment.xmlがあります。

幸運を!

于 2013-04-15T04:35:54.107 に答える