2014-02-14 52 views
2

我想讓這樣的事情:安卓:我不能讓collapseActionView

gmail menu collapsible

我儘量讓彈出菜單白衣collapseview,Y操作欄。但我嘗試了我在互聯網上找到的所有東西,並找不到解決方案。我沒有需要suppoart Android的API小於15

這個main.xml中

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
     android:id="@+id/action_settings" 
     android:orderInCategory="100" 
     android:showAsAction="never" 
     android:title="@string/action_settings"/> 
    <item android:id="@+id/action_search" 
      android:icon="@android:drawable/ic_dialog_email" 
      android:title="@string/hello_world" 
      android:showAsAction="always|collapseActionView" 
      /> 
    <item android:id="@+id/action_compose" 
      android:icon="@android:drawable/btn_star" 
      android:title="@string/hello_world" 
      android:showAsAction="never" 
      /> 
    <item android:id="@+id/action_compose2" 
      android:icon="@android:drawable/btn_star" 
      android:title="@string/hello_world" 
      android:showAsAction="never" 
      /> 
</menu> 

這是我的活動:

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ActionBar a = getActionBar(); 
     a.setTitle("Mariano"); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu items for use in the action bar 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main, menu); 
     MenuItem menuItem = menu.findItem(R.id.action_search); 

     MenuItemCompat.setOnActionExpandListener(menuItem, new OnActionExpandListener() { 

      @Override 
      public boolean onMenuItemActionExpand(MenuItem arg0) { 
       // TODO Auto-generated method stub 
       return false; 
      } 

      @Override 
      public boolean onMenuItemActionCollapse(MenuItem arg0) { 
       // TODO Auto-generated method stub 
       return false; 
      } 
     }); 

     return super.onCreateOptionsMenu(menu); 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     return false; 
    } 
} 

回答

3

如果我理解正確,您想通過點擊打開一個Android Popup Menu一個Button。然後,所有你需要做的就是把它添加到您的MainActivity

button1 = (Button) findViewById(R.id.button1); 
     button1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
     //Creating the instance of PopupMenu 
     PopupMenu popup = new PopupMenu(MainActivity.this, button1); 
     //Inflating the Popup using xml file 
     popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu()); 

     //registering popup with OnMenuItemClickListener 
     popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
     public boolean onMenuItemClick(MenuItem item) { 
       int i = item.getItemId(); 
         if (i == R.id.id1) { 
          //do something 
          return true; 
         } 
         else if (i == R.id.id2){ 
          //do something 
         } 
         else if (i == R.id.id3) { 
          //do something 
          return true; 
         } 
         else if (i == R.id.id4) { 
          //do something 
          return true; 
         }else { 
          return onMenuItemClick(item); 
         } 
     }); 

     popup.show();//showing popup menu 
     } 
     });//closing the setOnClickListener method 
    } 
+0

感謝您迴應我發現http://developer.android.com/guide/topics/ui/menus.html#PopupMenu – bomba1990

+0

高興幫助你:) – Rick

+0

我有一個傢伙與這個問題。什麼是android的唯一性:showAsAction =「always | collapseActionView」。 – bomba1990