2016-11-18 39 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);