Unable to display top right corner Menu bar in Android Studio1.3.1

Tell me what to do if I want a menu bar in the upper right corner of the action bar. And I use Studio Studio Studio 1.3.1. And the parameters described in the activity_main.xml file are given below: Nexus4 LIght MainActivity 21;

File 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>

And my MainActivity.java file:

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


}
}

Please give me ten points so that I can embed an image in it to understand the problem more clearly.

+4
source share
1 answer

try it

<!-- 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 display 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>
  1. onCreate() Activity

= ( ) findViewById (R.id.toolbar); setSupportActionBar ( );

0

Source: https://habr.com/ru/post/1621158/


All Articles