Android Development notes (9) -- initially set Menu and androidmenu
Reprinted Please note: http://www.cnblogs.com/igoslly/p/6858656.htmlSet Menu Set Menu and add button operation on ActionBar:Add the menu folder under the main/res directory and create the main. xml file.
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app=" http://schemas.android.com/apk/res/tool"> <item android:id ="@+id/coffee_cart" android:icon="@drawble/cart" android:title="Delete" app:showAsAction=”always”/> </menu>
ShowAsActionThe attribute has five values: ifRoom, never, always, withText, and collapseActionView.
IfRoom is displayed in Item, and the never will never be displayed based on the width of the screen. Only the title is displayed in the overflow list, so it is best to include the title when defining the item. Always are always displayed no matter whether they are overflow or not. The withText value displays the text title. If the icon is valid and restricted by space, the text title may not be displayed completely. The collapseActionView is folded into a button. The operation window is displayed when the user selects it. Generally, it works with ifRoom.
In MainActivity, You need to rewrite the onCreateOptionsMenu function.
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; }
Set the Button FunctionItem. getItemId () to obtain the ID of the click button. Because each item has a unique ID, use the switch-case-default statement to determine and execute the command
@Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){ case R.id. coffee_cart: Toast.makeText(this, "CoffeeCart", Toast.LENGTH_SHORT).show(); } }
Statement:
1. The text and pictures in this note are both original and original. For reprinted documents, enter "blog garden-igoslly ".
2, Android development courses in 2017 4 years to participate in Google developer learning, notes original http://www.studyjamscn.com/thread-20580-1-1.html#pid272486