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) {
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.
source
share