2016-11-18 38 views
1

我一直在嘗試將功能添加到我的Android應用程序,例如,當我點擊一個按鈕,菜單列表中應該可以看見:安卓:打開菜單編程

這裏是我的代碼:

菜單。 XML:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     tools:context="com.example.MainActivity" > 

    <item android:id="@+id/action_onthego_sentence" 
      android:title="settings" 
      android:orderInCategory="100" 
      app:showAsAction="never" /> 

</menu> 

從主要活動,在點擊一個按鈕,我做的:

 button.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View view) 
      { 
       runOnUiThread(new Runnable() 
       { 
        @Override 
        public void run() 
        { 
         openOptionsMenu(); 
        } 
       }); 
      } 
     }); 

我需要的是:

enter image description here

正如圖所示,我倒要看看菜單被打開。有什麼建議嗎?

+0

看看這個線程可能會幫助:http://stackoverflow.com/q/3133318/524160 3 –

回答

1

用途:

MainActivity.this.openOptionsMenu(); 

然後,檢查您的工具欄,如果是這樣行。

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.acercade_activity); 

     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayShowTitleEnabled(false); 
} 

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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()){ 
      //case R.id.action_settings: 
      // return true; 

      case R.id.perfil: 
       drawerLayout.openDrawer(Gravity.LEFT); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 



     } 

    } 
+0

是這個作品,如果在MainActivity由「活動」類延伸,但奇怪的是,如果它是由「AppCompatActivity」擴展,這是行不通的。任何想法呢? – KTB

+0

檢查新的更新,你的問題是工具欄。 @缺口 – josedlujan

0

在你的菜單佈局使用此:

<item 
android:id="@+id/action_settings" 
android:orderInCategory="100" 
android:title="Setting" 
app:showAsAction="ifRoom" /> 
0

嘗試以下解決方案這將是上的按鈕菜單列表中顯示的點擊應該是可見的:

RES /菜單/主。 XML

 <?xml version="1.0" encoding="utf-8"?> 
    <menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <item 
     android:id="@+id/action_changeLang" 
     android:orderInCategory="100" 
     android:title="Change Lang" /> 
    <item 
     android:id="@+id/action_color" 
     android:orderInCategory="100" 
     android:title="Select color" /> 
    <item 
     android:id="@+id/action_applist" 
     android:orderInCategory="100" 
     android:title="App List" /> 
</menu> 

MainActivity.java

 @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; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    switch (id) { 
     case R.id.action_changeLang: 
       // Add your code 
      break; 
     case R.id.action_color: 
      // Add your code 
      break; 
     case R.id.action_applist: 
      // Add your code 
      break; 
    } 
    return super.onOptionsItemSelected(item); 
    } 

嘗試使用上述解決方案。它會爲我

0

您好親愛的工作,如果你正在使用的工具欄下面的代碼

toolbar.showOverflowMenu(); 

與其他聰明人,你可以直接調用

MainActivity.this.openOptionsMenu(); 
3

如果你在你的應用程序中使用自定義工具欄,然後下面的方法將是有用的,

new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        toolbar.showOverflowMenu(); 
       } 
      }, 500);