如何将下拉菜单添加到Android项目的ActionBar中?
我一直在开发Android应用程序,但我对Android的4个以上版本了解不多。因此,请大家帮我 - 我与标签Android应用程序导航:如何将下拉菜单添加到Android项目的ActionBar中?
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
但我还需要下拉菜单添加到动作条的其他目标。我可以做吗?请,如果可能的话,给我一个例子。先谢谢你。
您可以使用一种称为android微调器的东西。它看起来像这样:
您可以自定义它以满足您的应用程序设计太多的方法。
这里是一个伟大的教程由谷歌如何使用这些:如果你想要将它添加到动作栏中
http://developer.android.com/guide/topics/ui/controls/spinner.html
,您可以通过微调适配器做如下详细说明: http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown
如果你想添加图标做某些动作,然后就可以看到这一点: http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems
如果你想d o在酒吧本身(如在谷歌搜索应用程序搜索)某些行为,然后看到: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
如果你想添加导航选项卡,然后看到: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs
绝对是最好的,和我发现迄今为止最简单的答案就是在这里。
基本上,在这种情况下不需要自定义布局。只需设置actonViewClass:
<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>
再处理它在onCreateOptionsMenu,像往常一样:
@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
s.setOnItemSelectedListener(onItemSelectedListener); // set the listener, to perform actions based on item selection
这是迄今为止最简单和干净的解决方案。对原作者FrançoisPOYER的致谢。
我知道这个元素的存在,但我不知道如何将微调添加到操作栏 – malcoauri
它不符合我的任务。请告诉我,如何将TextView或图标添加到ActionBar? – malcoauri
增加了一些资源来帮助你。您不能将常规textView和图标添加到操作栏,因为它是一个特殊的UI元素,而不是普通的布局。但是你可以添加某些东西来帮助你完成任务。 –