2012-01-12 66 views
1

我只是想給其他活動添加到我的選項菜單 我有我的應用程序,它顯示爲安卓:增加了選項菜單動態

public class OptionsMenuTesterActivity extends Activity { 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
     } 
     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      super.onCreateOptionsMenu(menu); 

      // Create an Intent that describes the requirements to fulfill, to be included 
      // in our menu. The offering app must include a category value of Intent.CATEGORY_ALTERNATIVE. 
      Intent intent = new Intent(null,getIntent().getData()); 
      intent.addCategory(Intent.CATEGORY_ALTERNATIVE); 

      // Search for, and populate the menu with, acceptable offering applications. 
      menu.addIntentOptions(
        Menu.CATEGORY_ALTERNATIVE, // Menu group 
       0,  // Unique item ID (none) 
       0,  // Order for the items (none) 
       this.getComponentName(), // The current Activity name 
       null, // Specific items to place first (none) 
       intent, // Intent created above that describes our requirements 
       0,  // Additional flags to control items (none) 
       null); // Array of MenuItems that corrolate to specific items (none) 

      return true; 
     } 
    } 

現在我已經創造了另一個簡單的應用程序有一個活動的菜單定義和它的androidmaifest看起來如下

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

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

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".DynamicOptionsMenuTesterActivity" 
      android:label="@string/app_name" > 
      <intent-filter label="test menu"> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
       <category android:name="android.intent.category.ALTERNATIVE" /> 

      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

我有兩個應用程序啓動。但是,當我點擊第一個應用程序的菜單,我什麼都看不到 你能幫我告訴我在這裏失蹤嗎?

回答

0
public void openOptionsMenu() { 
    // TODO Auto-generated method stub 
    super.openOptionsMenu(); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    //call the base class to include system menus 
    super.onCreateOptionsMenu(menu); 

     int group1 = 1; 
     MenuItem bakchome = menu.add(group1,1,1,"title"); 
     bakchome.setIcon(R.drawable.ic_menu_add); 

    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
     switch(item.getItemId()) { 
     case 1: 

     Intent intent1 = new Intent(getApplicationContext(), yourintent.class); 
     intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(intent1); 
      break; 

     } 

    return true; 
} 
0

問題是有點老,但我花了我一段時間才能完成它,所以也許它會爲別人節省一些時間和沮喪。希望它有幫助...

這是我做了什麼來創建我的動態菜單,提供所有可用的選項來共享圖像。我已經構建了自己的topmenu,而不是使用ActionBar。我的菜單項目開始於在ActivityonCreate方法的ImageButton ...

ImageButton shareBtn = (ImageButton)findViewById(R.id.shareBtn); 
    shareBtn.setOnClickListener(new View.OnClickListener() { 

     // receive launchables (to build menu items) 
     List<ResolveInfo> launchables = getLaunchablesToShareWith(); 
     PackageManager pm=getPackageManager(); 

     @Override 
     public void onClick(View v) { 
      //Creating the instance of PopupMenu 
      PopupMenu popup = new PopupMenu(getApplicationContext(), v); 

      //enable icons using Reflection... 
      try { 
       Field[] fields = popup.getClass().getDeclaredFields(); 
       for (Field field : fields) { 
        if ("mPopup".equals(field.getName())) { 
         field.setAccessible(true); 
         Object menuPopupHelper = field.get(popup); 
         Class<?> classPopupHelper = Class.forName(menuPopupHelper 
           .getClass().getName()); 
         Method setForceIcons = classPopupHelper.getMethod(
           "setForceShowIcon", boolean.class); 
         setForceIcons.invoke(menuPopupHelper, true); 
         break; 
        } 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 


      //Inflating the Popup using xml file 
      popup.getMenuInflater().inflate(R.menu.popupmenu_your_activity, popup.getMenu()); 

      // add items 
      for (ListIterator<ResolveInfo> iterator = launchables.listIterator(); iterator.hasNext();) { 

       // extract data from launchables 
       ResolveInfo resolveInfo = iterator.next(); 
       String title = resolveInfo.activityInfo.applicationInfo.loadLabel(pm).toString(); 
       String packageName = resolveInfo.activityInfo.applicationInfo.packageName; 
       Drawable appIcon = null; 
       try { 
        appIcon = pm.getApplicationIcon(packageName); 
       } catch (PackageManager.NameNotFoundException e) { 
        e.printStackTrace(); 
        continue; 
       } 

       // create & add item 
       popup.getMenu().add(0, 0, 0, title) 
           .setIcon(appIcon) 
           .setIntent(getShareIntent().setPackage(packageName)) 
           .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { 
            public boolean onMenuItemClick(MenuItem item) { 
             prepareImageToShare(true); 
             startActivity(item.getIntent()); 
             return true; 
            } 
           }); 
      } 

      popup.show(); 
     } 
    }); 

要創建launchables列表:

private List<ResolveInfo> getLaunchablesToShareWith() { 
    PackageManager pm = getPackageManager(); 

    Intent intent = new Intent(Intent.ACTION_SEND, null); 
    intent.setType("image/png"); 

    List<ResolveInfo> launchables=pm.queryIntentActivities(intent, 0); 
    Collections.sort(launchables, new ResolveInfo.DisplayNameComparator(pm)); 

    return launchables; 
} 

創建共享Intent

private Intent getShareIntent() { 
    // File helper returns something like return Uri.parse("file:///storage/emulated/0/YourApp/output/image.png"); 
    Uri imageUri = FileHelper.getOutputPathAsUri(Files.SHARED_OUTPUT_FILE.getFilename(), Paths.OUTPUT_FOLDER_PATH.getPath(), true); 

    Intent shareIntent = new Intent(Intent.ACTION_SEND); 
    shareIntent.setData(imageUri); 
    shareIntent.setType("image/png"); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri); 

    return shareIntent; 
} 

菜單佈局文件(popupmenu_your_activity.xml)看起來像這樣:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
tools:context="your.package.YourActivity" > 

    <!-- popup menu with dynamic content --> 

</menu>