2012-07-07 91 views
10

我添加了一个微调,使用第二个选项从答案here我动作条动作条(不导航。添加微调器

如何我添加一个微调适配器微调?我试图用一个微调对象作为谷歌描述here,但得到一个空的微调对象

任何人都知道如何做到这一点?我不希望微调器在操作栏的导航区域,但与其他行动项目(我正在使用拆分动作栏)

感谢您的帮助!

回答

6

嗯,我放弃了使用子菜单的Spinner想法。我意识到那个微调者是用来挑选一直保持选择的东西的;子菜单是一个更好的UI适合。

谢谢反正。

P.S. 我是新来的。如果回答我自己的问题让社区烦恼,请让我知道。我想成为一个好公民。我已经从你们那里得到了很多帮助。

15

我知道你抛弃了微调,但我会给一些暗示这里的情况下,其他人是有同样的问题,或者你来开发同样的模式在不同的应用程序

  • 如果你有空是因为你没有正确指定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 
    

快乐编码...

+0

任何想法这将如何工作的自定义适配器?我使用图片而不是文本字显示在Spinner中,所以行'SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(getActivity()。getActionBar() .getThemedContext(),R.array.my_menu_spinner_list,android.R。 'layout.simple_spinner_dropdown_item);'对于自定义适配器''必须不同。无法弄清楚如何。 – Azurespot 2015-05-25 09:45:57

+0

只需调用您在自定义适配器上的任何构造函数即可。 'SpinnerAdapter mSpinnerAdapter = new MyCustomAdapter()' – Budius 2015-05-25 09:47:55

+0

感谢Budius,我是这么做的,但由于某种原因,下拉不会发生。我想这是因为'android.R.layout.simple_spinner_dropdown_item'在我的自定义适配器中缺失(因为它使用Spinner行作为ImageView,drawables数组进入的地方)。但不知道这是否是问题。如果你想看看我的代码:http://stackoverflow.com/questions/30433501/custom-spinner-not-showing-dropdown-in-actionbar-icon这个问题还没有答案。谢谢。 – Azurespot 2015-05-25 10:05:16

27

我知道这是一个老问题,但以防万一有人绊倒它(像我一样),并且仍然在寻找完整的答案,下面介绍如何使用兼容性库进行操作,使其可以从v7(Android 2.1 Eclair)到当前的v19(Android 4.4 KitKat):

在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> 

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

然后在您的活动代码:

@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瞧!

+1

如果您不想担心旧版本,那么yourapp命名空间不是必需的,您可以使用android命名空间。此外,这与CursorLoaders完美结合。 – lalitm 2014-04-24 05:07:54

+0

由于目前Android 2.3.3(API v10)仍占android市场的18%(请参阅[Android Dashboard](http://developer.android.com/about/dashboards/index.html)数字),我认为提供兼容性答案很重要。但是如果您的应用程序的最小目标sdk高于Android 3.0(API v11),那么您的android命名空间就足够了。 – 2014-04-28 13:02:52

+0

@lalitm如果您注意到了,它是针对兼容性库的。如果你的目标是成为BC,这肯定是必需的。 – frostymarvelous 2016-02-17 13:50:02

-2
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