【发布时间】:2016-05-08 03:14:19
【问题描述】:
我正在开发一个包含一些活动的简单应用程序。所以当我启动应用程序并进入我的 MainActivity 时,我有我的操作栏,一切都很好。该 MainActivity 中有 2 个按钮,单击它们可将您带到其他活动。我想要做的是我想为那些次要活动实现一个操作栏。我已经尝试了所有可以在 google 或 youtube 上找到的东西,但没有任何效果。希望你们能帮助我。我希望将 BACK 按钮放在该操作栏上,这将带我回到 activity_main 但当然我首先需要一个操作栏。我将为您提供我的一项次要活动的代码,以便您可以建议我添加什么。
牛顿活动
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"
/>
【问题讨论】:
标签: android android-activity android-actionbar android-actionbaractivity