简体   繁体   中英

Call onCreateOptionsMenu method when app starts

I'm new in Android programming. I'm trying to create a login form app with options menu.

When the app starts it shows only welcome.xml. But I also want the menu on the top.

How and when I should call 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:

这是应用程序启动时显示的内容:

Add Toolbar in your 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

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() will call after onCreate() but before onCreate get finished. You do not need to call it manually.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM