Android 将微调器添加到ActionBar(非导航)

Android 将微调器添加到ActionBar(非导航),android,xml,layout,spinner,android-actionbar,Android,Xml,Layout,Spinner,Android Actionbar,我已经使用答案中的第二个选项在ActionBar中添加了一个微调器 如何将微调器适配器添加到微调器?我尝试使用Google描述的微调器对象,但得到的微调器对象为空 有人知道怎么做吗?我不希望微调器位于操作栏的导航区域,而是与其他操作项一起(我使用的是拆分操作栏) 感谢您的帮助!好吧,我放弃了使用子菜单的微调器想法。我意识到微调器是用来选择保持选中状态的内容的;子菜单接缝是为了更好地适应用户界面。我知道您放弃了微调器,但我会在这里给出一些提示,以防其他人遇到同样的问题或您来开发相同的pa另一个应用

我已经使用答案中的第二个选项在ActionBar中添加了一个微调器

如何将微调器适配器添加到微调器?我尝试使用Google描述的微调器对象,但得到的微调器对象为空

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


感谢您的帮助!

好吧,我放弃了使用子菜单的微调器想法。我意识到微调器是用来选择保持选中状态的内容的;子菜单接缝是为了更好地适应用户界面。

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

  • 如果为空是因为没有正确指定ID。请仔细检查ID
  • 在通过指定actionLayout(它只是一个微调器)显示的复杂内容的链接中,您只需指定一个actionViewClass=“android.widget.spinner”即可
  • 然后在创建选项菜单中执行以下操作:

    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
    

快乐编码…

我知道这是一个老问题,但为了防止有人无意中发现它(正如我所做的)并仍在寻找完整的答案,下面是如何使用兼容性库来实现它,以便它从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>

Et voilía!

如果你不想担心旧版本,那么你的应用程序名称空间就不是必需的,你可以使用android名称空间。此外,这在游标加载程序上也非常有效。因为目前android 2.3.3(API v10)仍然占安卓市场的约18%(有关当前的数字,请参阅),我认为提供兼容性答案很重要。但是,如果您的应用程序最低目标sdk高于android 3.0(API v11),那么android名称空间就足够了,这一点您是正确的@lalitm如果你注意到了,这是为了兼容库。如果你想成为BC,这绝对是必需的。关于这一点的警告:在Android版本6.0.1的Nexus 6上进行测试-我发现如果ActionBar上没有空间放置微调器,它将不会显示选项。它在正常的菜单位置不起作用。请参考以显示AsAction设置。因此,在小屏幕上,它可能会导致控件损坏。好消息:showaAction=“ifRoom | collapseActionView”在菜单和操作栏上似乎都能正常工作…我建议您使用这种组合,以防您的用户有一个小屏幕。您知道自定义适配器如何工作吗?我使用图像而不是文本字来显示在微调器中,因此行
SpinnerAdapter mSpinnerAdapter=ArrayAdapter.createFromResource(getActivity().getActionBar().getThemedContext(),R.array.my_菜单_微调器_列表,android.R.layout.simple_微调器_下拉菜单_项);
对于自定义适配器必须不同。不知道如何。只需调用自定义适配器上的任何构造函数即可。
微调器mSpinnerAdapter=new MyCustomAdapter()
谢谢Budius,我这么做了,但出于某种原因,下拉列表没有出现。我想这是因为
android.R.layout.simple\u spinner\u dropdown\u item
在我的自定义适配器中丢失了(因为它使用了一个微调器行作为ImageView,drawables数组进入的位置).但不确定这是否是问题所在。如果您想查看我的代码:问题仍然没有答案。谢谢。
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
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