0

我需要有一个菜单或操作栏,当用户使用应用程序时必须显示,但问题是菜单项不只显示,我单击模拟器上的菜单按钮有人可以帮助我吗????

测试.java

package com.example.test;

import com.example.test.R;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class test extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_activity_menu, menu);
        return true;
    }

}

mani_activity_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_settings"
        android:orderInCategory="1"
        android:showAsAction="always"
        android:title="settings"/>
    <item
        android:id="@+id/twitter"
        android:orderInCategory="2"
        android:showAsAction="always"
        android:title="twitter"/>
    <item
        android:id="@+id/facebook"
        android:orderInCategory="3"
        android:showAsAction="always"
        android:title="facebook"/>
    <item 
        android:id="@+id/refresh"
        android:title="refresh"
        android:showAsAction="always"
        android:orderInCategory="4"
        />


</menu>

显现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".test"
            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>

</manifest>

样式.xml

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>
4

1 回答 1

0

试过你的代码。它的工作没有任何问题

您在 mani_activity_menu.xml 中犯了拼写错误?

添加

setHasOptionsMenu(true);//在oncreate中

item.setEnabled(false);//在oncreated选项中添加这个

  package com.example.exammmm;



   import android.os.Bundle;

   import android.app.Activity;
   import android.view.Menu;

    public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

   }

在此处输入图像描述
(来源:mediafire.com

阅读-----> http://developer.android.com/guide/topics/ui/actionbar.html

于 2013-09-20T17:14:56.987 回答