简体   繁体   English

设置Android的ActionBar菜单项

[英]Setting ActionBar Menu Items Android

I am just getting started with Android. 我刚开始使用Android。 I want to add an ActionBar menu to my small tip calculator application. 我想将ActionBar菜单添加到我的小技巧计算器应用程序中。 I have set up the menu xml file. 我已经设置了菜单xml文件。 When I run the application on my mobile device, the part where the menu is supposed to appear, is blank. 当我在移动设备上运行该应用程序时,应该显示菜单的部分为空白。

My onCreate Method does not have the setActionBar() method, so I think the problem could lie there?? 我的onCreate方法没有setActionBar()方法,所以我认为问题可能出在那儿?

Here is the code for my onCreate Method: 这是我的onCreate方法的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    serviceLevelSpinner = (Spinner) findViewById(R.id.serviceLevelSpinner);

Menu.xml Menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools">
<item android:id = "@+id/aboutLink" android:title = "@string/applicationAbout" app:showAsAction = "always" android:icon = "@drawable/ic_mymenu"/>
</menu>

Check whether you extends the ActionbarActivity to make the Activity or simple Activity . 检查您是否扩展ActionbarActivity以创建Activity或简单Activity To has the functionality of Actionbar you need to extends the ActionbarActivity . 要具有Actionbar的功能,您需要扩展 ActionbarActivity And then your code will work fine with the menu items. 然后,您的代码将与菜单项一起正常工作。

First ensure that your Activity extends AppCompatActivity . 首先确保您的活动扩展了AppCompatActivity Next, add a Toolbar to your Activity's layout file. 接下来,将工具栏添加到“活动”的布局文件中。 Call the setSupportActionBar(toolbar) method in onCreate . onCreate中调用setSupportActionBar(toolbar)方法。 The parameter used should be an instance of the Toolbar from your layout. 使用的参数应该是布局中“工具栏”的一个实例。

Note that in your styles.xml file, you have to set the windowActionBar item to false or use a theme with parent="Theme.AppCompat.NoActionBar" to avoid errors from duplicate toolbars. 请注意,在styles.xml文件中,必须将windowActionBar项设置为false或将主题与parent =“ Theme.AppCompat.NoActionBar”一起使用,以避免重复的工具栏出现错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM