添加微调器
回答
嗯,我放弃了使用子菜单的Spinner想法。我意识到那个微调者是用来挑选一直保持选择的东西的;子菜单是一个更好的UI适合。
谢谢反正。
P.S. 我是新来的。如果回答我自己的问题让社区烦恼,请让我知道。我想成为一个好公民。我已经从你们那里得到了很多帮助。
我知道你抛弃了微调,但我会给一些暗示这里的情况下,其他人是有同样的问题,或者你来开发同样的模式在不同的应用程序
- 如果你有空是因为你没有正确指定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
快乐编码...
我知道这是一个老问题,但以防万一有人绊倒它(像我一样),并且仍然在寻找完整的答案,下面介绍如何使用兼容性库进行操作,使其可以从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瞧!
如果您不想担心旧版本,那么yourapp命名空间不是必需的,您可以使用android命名空间。此外,这与CursorLoaders完美结合。 – lalitm 2014-04-24 05:07:54
由于目前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
@lalitm如果您注意到了,它是针对兼容性库的。如果你的目标是成为BC,这肯定是必需的。 – frostymarvelous 2016-02-17 13:50:02
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
- 1. Angular2 - 如何添加加载微调器?
- 2. 动态数据添加到微调器,但是,微调
- 3. 如何添加微调器到AlertDialog.Builer?
- 4. 动态添加微调器到RelativeLayout
- 5. 在小部件中添加微调器
- 6. 动态添加jQuery UI微调器
- 7. Flex数字微调器,想添加0
- 8. 如何添加微调器到RecyclerView
- 9. 如何为微调器添加空间?
- 10. 在片段中添加微调器
- 11. 如何添加自定义微调器?
- 12. 将微调器添加为ListView数据
- 13. 添加分隔微调项
- 14. 制作加载微调器
- 15. 添加加入登录微调(jquery)
- 16. 如何添加一个加载“微调”
- 17. 如何在Ajax调用上添加UI微调器?
- 18. 在新图像完全加载时添加微调器
- 19. 如何在异步路由加载时添加微调器?
- 20. 添加加载微调器的WebView的Andriod应用
- 21. 如何将加载微调器添加到WooCommerce结帐页面
- 22. 在Ajax调用上加载微调器
- 23. Android微调项目添加项目
- 24. Android微调添加/删除条目
- 25. 添加类与微调之前继续
- 26. 俊男项目添加到微调
- 27. 微调项目添加项目
- 28. listview在每一行添加微调框?
- 29. 如何添加微调到ActionBar?
- 30. 如何添加取消内部微调
任何想法这将如何工作的自定义适配器?我使用图片而不是文本字显示在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
只需调用您在自定义适配器上的任何构造函数即可。 'SpinnerAdapter mSpinnerAdapter = new MyCustomAdapter()' – Budius 2015-05-25 09:47:55
感谢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