我通过Setting the tab navigation in action bar
and实现了这一点Setting Spinner in Menu for navigation list
。我会解释第二部分,因为第一部分是微不足道的。
定义Spinner
在action_bar.xml
:
<item
android:id="@+id/menu_test_spinner"
android:actionViewClass="android.widget.Spinner"
android:showAsAction="always"/>
在活动中设置和绑定数据Spinner
:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_bar, menu);
MenuItem testSpinner = menu.findItem( R.id.menu_test_spinner );
setupTestSpinner(testSpinner);
return true;
}
private void setupTestSpinner(MenuItem item) {
View view = item.getActionView();
if (view instanceof Spinner) {
Spinner spinner = (Spinner) view;
spinner.setAdapter(ArrayAdapter.createFromResource(this,
R.array.test,
android.R.layout.simple_spinner_dropdown_item));
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
//Do something on item selection
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
现在,重写将响应选项卡更改的方法,并更改 Spinner 中的导航列表项。