android - 在 activity_main 之外的其他 Activity 中添加操作栏

标签 android android-activity android-actionbar android-actionbaractivity

我正在开发一个简单的应用程序,其中包含一些 Activity 。所以当我启动应用程序并进入我的 MainActivity 时,我有我的操作栏,一切都很好。该 MainActivity 中有 2 个按钮,单击这些按钮会将您带到其他 Activity 。我想做的是对那些次要 Activity 实现操作栏。我已经尝试了在 google 或 youtube 上可以找到的所有内容,但没有任何效果。希望你们能帮助我。我希望在该操作栏上放置 BACK 按钮,它将带我回到 activity_main 但当然我首先需要一个操作栏。我会给你我的一个次要 Activity 的代码,这样你就可以建议我添加什么。

牛顿 Activity

public class NewtonScreen extends Activity
{
     private Button theAnswerButton, theHintButton, theSuckButton;

@Override
  protected void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);

     setContentView(R.layout.newton_layout);

     theAnswerButton = (Button) findViewById(R.id.answer_button);
     theHintButton = (Button) findViewById(R.id.hint_button);
     theSuckButton = (Button)findViewById(R.id.suck_at_physics_button);


}

/**@Override
     public boolean onCreateOptionsMenu(Menu menu) {
     //Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.newton_menu, menu);
          return true;
}

@Override
  public boolean onOptionsItemSelected(MenuItem item) {
     // Handle action bar item clicks here. The action bar will
     // automatically handle clicks on the Home/Up button, so long
     // as you specify a parent activity in AndroidManifest.xml.
     int id = item.getItemId();

     //noinspection SimplifiableIfStatement
     if (id == R.id.action_settings) {
         return true;
    }

      return super.onOptionsItemSelected(item);
}**/

// Some other methods that are not important for this issue

如您所见,我已尝试放置这 2 个 Override 方法,我什至创建了一个 newton_menu 布局,但它似乎不起作用。

牛顿菜单

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".NewtonScreen">

<item 
    android:id="@+id/newton_action_exit"
    android:title="@string/action_exit"
    android:orderInCategory="101"
    app:showAsAction="ifRoom"
    />

最佳答案

extends Activity 更改为 extend AppCompatActivity

在oncreate中调用这段代码

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

和这段代码

    @Override
public boolean onOptionsItemSelected(int featureId, MenuItem item) {
     int itemId = item.getItemId();
     if(itemId == android.R.id.home){
         finish();
     }
     return true;
}

关于android - 在 activity_main 之外的其他 Activity 中添加操作栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35100559/

相关文章:

android - 重写 onNewIntent 时是否有任何理由不调用 setIntent?

android - 如果服务正在运行,如何启动不同的 Activity (并恢复状态)?

android - ActionBar 中的选项卡

android - android 样式 :uiOptions ="splitActionBarWhenNarrow"?

Android Studio - 找不到汽车模拟器

java - 安卓 : Least recently used (LRU) algorithm implementation in java?

android - 替换 fragment 的最佳方法

javascript - 从单独的类调用 Activity 函数

Android 工具栏覆盖了一个 fragment

android - emulator-5554 在屏幕上不可见?