2015-12-21 108 views
4

请告诉如果我想要在活动栏右上角的菜单栏中执行操作。 而我正在使用Android Studio studio 1.3.1 并且activity_main.xml设计选择的选项如下: Nexus4 LIght MainActivity 21;无法显示Android Studio中的右上角菜单栏1.3.1

我menu_main.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=".MainActivity"> 
<group android:checkableBehavior="single"> 
    <item 
     android:id="@+id/menu_red" 
     android:orderInCategory="1" 
     app:showAsAction="never" 
     android:title="@string/red_string"/> 
    <item 
     android:id="@+id/menu_green" 
     android:orderInCategory="2" 
     app:showAsAction="never" 
     android:title="@string/green_string"/> 
    <item 
     android:id="@+id/menu_yellow" 
     android:orderInCategory="3" 
     app:showAsAction="never" 
     android:title="@string/yellow_string"/> 
</group> 

而我MainActivity.java文件是:

package abcd.shravankr.basic; 

import android.graphics.Color; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.RelativeLayout; 


public class MainActivity extends ActionBarActivity { 

@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.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    RelativeLayout main_view=(RelativeLayout)findViewById(R.id.main_view); 
    switch (item.getItemId()) { 
     case R.id.menu_red: 
      if(item.isChecked()) 
       item.setChecked(false); 
      else 
       item.setChecked(true); 
      main_view.setBackgroundColor(Color.RED); 
      return true; 
     case R.id.menu_green: 
      if(item.isChecked()) 
       item.setChecked(false); 
      else 
       item.setChecked(true); 
      main_view.setBackgroundColor(Color.GREEN); 
      return true; 
     case R.id.menu_yellow: 
      if(item.isChecked()) 
       item.setChecked(false); 
      else 
       item.setChecked(true); 
      main_view.setBackgroundColor(Color.YELLOW); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 


} 
} 

请给我十个点,这样我可以插入图像,理解问题更清楚。

+0

我已经改变主题的许多倍。但没有主题显示三个虚线的菜单栏选项。 –

+0

尝试应用程序:showAsAction =“always” –

回答

0

试试这个

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 

    <item name="alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item> 
    <item name="windowActionBar">false</item> 
    <item name="actionOverflowButtonStyle">@style/OverFlow</item> 
</style> 

<style name="OverFlow" parent="Widget.AppCompat.ActionButton.Overflow"> 
    <item name="android:src">@drawable/overflow</item> 
</style> 

actionOverflowButtonStyle is used to show the custom overflow menu icon.

编辑::
1.拓展业务类AppCompactActivity
2.添加tollbar到你的XML布局文件。

<android.support.v7.widget.Toolbar   
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/toolbar_top" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/colorPrimary" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="@android:style/Theme.Light"> 

    </android.support.v7.widget.Toolbar> 
  • 在的onCreate()活动
  • Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);

    +0

    何处放置此代码@Deepak –

    +0

    在styles.xml中将此用作您的样式,或者您可以提供styles.xml文件以便我可以看到它。 –

    +0

    很多在代码中显示的错误。你提供 –

    相关问题