应用启动时调用onCreateOptionsMenu方法

时间:2015-12-08 15:54:31

标签: java android xml

我是Android编程的新手。我尝试使用选项菜单创建登录表单应用程序。

当应用程序启动时,它只显示welcome.xml。但我也想把菜单放在最上面。

我应该如何以及何时调用onCreateOptionsMenu()?

的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

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

<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文件

<?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)

<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:

2 个答案:

答案 0 :(得分:0)

在welcome.xml中添加Toolbar

<强>的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>

然后从您的活动

中将工具栏设置为ActionBar

<强> 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);
    }

 }
  

我应该如何以及何时调用onCreateOptionsMenu()?

onCreateOptionsMenu()将在onCreate()之后调用但在onCreate完成之前调用。您无需手动调用它。

答案 1 :(得分:0)

您应该尝试在onCreate中调用setHasOptionsMenu(true):