Java 应用程序启动时调用onCreateOptions菜单方法
我是Android编程新手。我正在尝试创建一个带有选项菜单的登录表单应用程序 当应用程序启动时,它只显示welcome.xml。但我还想把菜单放在最上面 如何以及何时调用onCreateOptions菜单() AndroidManifest.xmlJava 应用程序启动时调用onCreateOptions菜单方法,java,android,xml,Java,Android,Xml,我是Android编程新手。我正在尝试创建一个带有选项菜单的登录表单应用程序 当应用程序启动时,它只显示welcome.xml。但我还想把菜单放在最上面 如何以及何时调用onCreateOptions菜单() AndroidManifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="it.fruci.davide.firstapplication"> <a
<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>
<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>
<?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>
<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>
welcome.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>
<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>
<?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>
<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>
menu.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>
<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>
<?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>
<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>
styles.xml(@style/AppTheme)
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
这是应用程序启动时显示的内容:
添加your welcome.xml
welcome.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>
<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>
<?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>
<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>
如何以及何时调用onCreateOptions菜单()
oncreateoptions菜单()
将在onCreate()之后但在onCreate完成之前调用。您不需要手动调用它。添加your welcome.xml
welcome.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>
<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>
<?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>
<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>
如何以及何时调用onCreateOptions菜单()
oncreateoptions菜单()
将在onCreate()之后但在onCreate完成之前调用。您不需要手动调用它。您应该尝试在onCreate中调用SetHasOptions菜单(true);您应该尝试在onCreate中调用SetHasOptions菜单(true);我觉得一切正常。。。您正在显示操作栏吗?您的AppTheme的父项是什么?请同时添加@style/AppTheme xmlHi@npace,您对我的AppTheme的父项是什么意思?我用应用程序的截图更新了这个问题。你在测试哪个版本的Android?试着按下电脑上的菜单按钮device@fractalwrench安卓4.4.2。哦按下菜单按钮,菜单出现!非常感谢。但是,如果我想把菜单放在上面?我觉得一切都很好。。。您正在显示操作栏吗?您的AppTheme的父项是什么?请同时添加@style/AppTheme xmlHi@npace,您对我的AppTheme的父项是什么意思?我用应用程序的截图更新了这个问题。你在测试哪个版本的Android?试着按下电脑上的菜单按钮device@fractalwrench安卓4.4.2。哦按下菜单按钮,菜单出现!非常感谢。但是,如果我想把菜单放在上面?