如何在Android中将ActionBar添加到Activity [英] How to add ActionBar to Activity in Android

查看:140
本文介绍了如何在Android中将ActionBar添加到Activity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将ActionBar添加到我的android活动中,但是由于某些代码错误,应用程序自动关闭.那么在Activity中显示ActionBar的正确代码是什么?

I am trying to add ActionBar to my android activity, but app automatically closes due to some code error. So what is the proper code to display ActionBar in the Activity?

我在onCreate方法中尝试了此代码,但无法正常工作

I tried this code in onCreate method but its not working

ActionBar actionBar;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timetable);

   actionBar = getActionBar();
   actionBar.setTitle("MY TITLE");
   actionBar.show();
 }

推荐答案

尝试一下,

  • 请勿使用ActionBar使用Toolbar.
  • 将类范围设为AppCompatActivity,而不是ActionBarActivity.
  • 使用getSupportActionBar()代替getActionBar().
  • 默认情况下显示Toolbar,因此您无需调用show()方法.
  • Don't use the ActionBar use Toolbar.
  • Make your class extent AppCompatActivity instead of ActionBarActivity.
  • Use getSupportActionBar() instead of getActionBar().
  • A Toolbar is shown by default, so you dont nedd to call the show() method.

Android工具栏示例

这篇关于如何在Android中将ActionBar添加到Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆