我有导航菜单项。目前我在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定义它?
答案 0 :(得分:0)
你需要做两件事:一件是设置xml,第二件是给它充气。这是 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。
祝你好运!