我有以下代码已经创建了一个下拉菜单
public class MainActivity extends FragmentActivity implements
ActionBar.OnNavigationListener {
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar to show a dropdown list.
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setDisplayShowHomeEnabled(false);
// Set up the dropdown list navigation in the action bar.
actionBar.setListNavigationCallbacks(
// Specify a SpinnerAdapter to populate the dropdown list.
new ArrayAdapter<String>(actionBar.getThemedContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1, new String[] {
getString(R.string.demo1),
getString(R.string.demo2),
getString(R.string.demo3)}), this);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Restore the previously serialized current dropdown position.
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
getActionBar().setSelectedNavigationItem(
savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
// Serialize the current dropdown position.
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
.getSelectedNavigationIndex());
}
@Override
public boolean onNavigationItemSelected(int position, long id) {
// When the given dropdown item is selected, show its contents in the
// container view.
Fragment fragment = new MainFragmentSection();
Bundle args = new Bundle();
args.putInt(MainFragmentSection.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, fragment).commit();
return true;
}
public static class MainFragmentSection extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public MainFragmentSection() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main_fragment,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return rootView;
}
}
}
我想要做的是在现有的下拉菜单旁边的同一操作栏中添加另一个下拉列表,以便在用户选择时我可以重新绘制Focus中的当前片段(这取决于第一个)微调器或下拉菜单)有一些小的改动,比如在当前片段视图中对字符串数组进行排序。
非常感谢一些指导和帮助。
答案 0 :(得分:1)
我建议不要这样做,因为操作栏是Android中的标准设计模式,并且自定义它会在应用程序之间产生不一致,这会使用户感到困惑/沮丧。但是我知道有时你会因为各种原因被迫这样做。
因此,您可以为操作栏创建自定义视图。这允许您根据需要进行设计。这是一个vogella教程,解释了如何:
http://www.vogella.com/articles/AndroidActionBar/article.html#menu_customviews
基本上,您使用XML创建自定义视图,然后使用代码或主题样式将其添加到操作栏。
希望有所帮助。