Android 向 ActionBar 添加微调器(不是导航

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11377760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:13:10  来源:igfitidea点击:

Adding spinner to ActionBar (not Navigation

androidxmllayoutspinnerandroid-actionbar

提问by user1509130

I have added a spinner to my ActionBar using the second option from the answer here.

我使用这里答案中的第二个选项向我的 ActionBar 添加了一个微调器。

How to I add a spinner adapter to the spinner? I tried using a Spinner object as Google describes herebut get a null Spinner object.

如何向微调器添加微调器适配器?我尝试使用 Google在此处描述的 Spinner 对象,但得到了一个空 Spinner 对象。

Anybody know how to do this? I don't want the spinner to be in the navigation area of the action bar but in with the other action items (I am using the split action bar).

有人知道怎么做吗?我不希望微调器位于操作栏的导航区域中,而是与其他操作项一起(我正在使用拆分操作栏)。

Thanks for the help!

谢谢您的帮助!

采纳答案by user1509130

Well, I ditched the Spinner idea for using a submenu. I realized that the spinner was for selecting things that stayed selected; submenus seamed to be a better UI fit.

好吧,我放弃了使用子菜单的 Spinner 想法。我意识到微调器是用来选择一直被选中的东西;子菜单缝合以更好地适应用户界面。

回答by Fran?ois POYER

I know this is an old question, but just in case someone stumbles on it (as I did) and still looks for a complete answer, here is how to do it using the compatibility library, so that it works from to v7 (Android 2.1 Eclair) to current v19 (Android 4.4 KitKat):

我知道这是一个老问题,但以防万一有人偶然发现它(就像我一样)并且仍然在寻找完整的答案,这里是如何使用兼容性库来做到这一点,以便它可以从 v7(Android 2.1) Eclair)到当前 v19(Android 4.4 KitKat):

In menu_layout.xml:

在 menu_layout.xml 中:

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

  <item android:id="@+id/spinner"
    yourapp:showAsAction="ifRoom"
    yourapp:actionViewClass="android.widget.Spinner" />
</menu>

Using http://schemas.android.com/apk/res-autonamespace aliased as yourappenables you to use the attributes showAsAction and actionViewClass that do not exist on older versions of Android.

使用http://schemas.android.com/apk/res-auto别名为 as 的命名空间,yourapp您可以使用旧版 Android 上不存在的属性 showAsAction 和 actionViewClass。

Then in your Activity code:

然后在您的活动代码中:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_layout, menu);
    MenuItem item = menu.findItem(R.id.spinner);
    Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
    spinner.setAdapter(adapter); // set the adapter to provide layout of rows and content
    spinner.setOnItemSelectedListener(onItemSelectedListener); // set the listener, to perform actions based on item selection

Et voilà!

等等!

回答by Budius

I know you ditched the spinner, but I'll give some hints here in case other people is having the same problem or you come to develop the same pattern in a different app

我知道你放弃了微调器,但我会在这里给出一些提示,以防其他人遇到同样的问题,或者你来在不同的应用程序中开发相同的模式

  • If you got null is because you didn't properly specify the IDs. Double check the IDs.
  • on of the links you showed over complicated stuff by specifying a actionLayout that is just a spinner, you can just specify an actionViewClass="android.widget.Spinner" that will do the trick.
  • then in the OnCreateOptionsMenuyou do:

    inflater.inflate(R.menu.my_menu, menu); // inflate the menu
    Spinner s = (Spinner) menu.findItem(R.id.my_menu_spinner).getActionView(); // find the spinner
    SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(getActivity().getActionBar()
            .getThemedContext(), R.array.my_menu_spinner_list, android.R.layout.simple_spinner_dropdown_item); //  create the adapter from a StringArray
    s.setAdapter(mSpinnerAdapter); // set the adapter
    s.setOnItemSelectedListener(myChangeListener); // (optional) reference to a OnItemSelectedListener, that you can use to perform actions based on user selection
    
  • 如果您得到 null 是因为您没有正确指定 ID。仔细检查 ID。
  • 在您通过指定一个只是一个微调器的 actionLayout 显示的复杂内容的链接上,您可以指定一个 actionViewClass="android.widget.Spinner" 来解决这个问题。
  • 然后在OnCreateOptionsMenu你做:

    inflater.inflate(R.menu.my_menu, menu); // inflate the menu
    Spinner s = (Spinner) menu.findItem(R.id.my_menu_spinner).getActionView(); // find the spinner
    SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(getActivity().getActionBar()
            .getThemedContext(), R.array.my_menu_spinner_list, android.R.layout.simple_spinner_dropdown_item); //  create the adapter from a StringArray
    s.setAdapter(mSpinnerAdapter); // set the adapter
    s.setOnItemSelectedListener(myChangeListener); // (optional) reference to a OnItemSelectedListener, that you can use to perform actions based on user selection
    

happy coding...

快乐编码...

回答by Avinash Gautam

inflater.inflate(R.menu.my_menu, menu); // inflate the menu 

Spinner s = (Spinner) menu.findItem(R.id.my_menu_spinner).getActionView();     // find the spinner 
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(getActivity().getActionBar() .getThemedContext(), R.array.my_menu_spinner_list, android.R.layout.simple_spinner_dropdown_item);    // create the adapter from a StringArray 
s.setAdapter(mSpinnerAdapter);   // set the adapter 
s.setOnItemSelectedListener(myChangeListener);    // (optional) reference to a OnItemSelectedListener, that you can use to perform actions based on user selection