将微调器添加到 ActionBar(而不是导航

2024-02-21

我使用答案中的第二个选项向我的 ActionBar 添加了一个微调器here https://stackoverflow.com/questions/8312344/how-to-add-a-dropdown-item-on-the-action-bar .

如何向微调器添加微调器适配器?我尝试使用 Google 描述的 Spinner 对象here http://developer.android.com/guide/topics/ui/controls/spinner.html但得到一个 null Spinner 对象。

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

谢谢您的帮助!


我知道这是一个老问题,但以防万一有人偶然发现它(就像我所做的那样)并且仍在寻找完整的答案,这里是如何使用兼容性库来做到这一点,以便它可以从 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>

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

然后在您的活动代码中:

@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

瞧!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将微调器添加到 ActionBar(而不是导航 的相关文章

随机推荐