gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:10:19 24 4
gpt4 key购买 nike

我正在开发一个简单的应用程序,其中包含一些 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com