[英]Call onCreateOptionsMenu method when app starts
I'm new in Android programming. 我是Android编程的新手。 I'm trying to create a login form app with options menu.
我正在尝试使用选项菜单创建登录表单应用程序。
When the app starts it shows only welcome.xml. 应用启动时,仅显示welcome.xml。 But I also want the menu on the top.
但我也希望菜单位于顶部。
How and when I should call onCreateOptionsMenu()? 我应该如何以及何时调用onCreateOptionsMenu()?
AndroidManifest.xml AndroidManifest.xml中
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.fruci.davide.firstapplication">
<application android:allowBackup="true" android:label="@string/app_name"
android:icon="@mipmap/ic_launcher" android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainClass"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainClass.java MainClass.java
public class MainClass extends Activity {
@Override
public void onCreate(Bundle saveOnInstanceState){
super.onCreate(saveOnInstanceState);
setContentView(R.layout.welcome);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
}
welcome.xml 的welcome.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username:"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:id="@+id/username" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:id="@+id/password" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:onClick="login"
android:text="Login"/>
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:onClick="cancel"
android:text="Cancella"/>
</TableRow>
</TableLayout>
menu.xml menu.xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/MENU_1"
android:title="Nuova nota"/>
<item
android:id="@+id/MENU_2"
android:title="Elenco note"/>
</menu>
styles.xml (@style/AppTheme) styles.xml(@ style / AppTheme)
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
This is what is shown when the application starts: 这是应用程序启动时显示的内容:
Add Toolbar in your welcome.xml 在welcome.xml中添加工具栏
welcome.xml 的welcome.xml
<RelativeLayout
xmlns:android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<Android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"></Android.support.v7.widget.Toolbar>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout-below="@+id/toolbar">
......
</TableLayout>
</RelativeLayout>
Then set Toolbar as an ActionBar from your Activity 然后在“活动”中将工具栏设置为ActionBar
MainClass.java MainClass.java
public class MainClass extends Activity {
@Override
public void onCreate(Bundle saveOnInstanceState){
super.onCreate(saveOnInstanceState);
setContentView(R.layout.welcome);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
How and when I should call onCreateOptionsMenu()?
我应该如何以及何时调用onCreateOptionsMenu()?
onCreateOptionsMenu()
will call after onCreate()
but before onCreate get finished. onCreateOptionsMenu()
将在onCreate()
但在onCreate完成之前调用。 You do not need to call it manually. 您不需要手动调用它。
您应该尝试在onCreate中调用setHasOptionsMenu(true):。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.