Android Learning: Actionbar activity Bar

Source: Internet
Author: User

Menu definition:

<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:title= "@string/font_size"android:showasaction= "Always|withtext"Android:icon= "@drawable/font" > <menu> <!--define a set of single-selection menu items--<group Android:checkablebehavi Or= "single" > <!--define multiple menu items--<Item Android:id= "@+id/font_10"Android:title= "@string/font_10"/> <Item Android:id= "@+id/font_12"Android:title= "@string/font_12"/> <Item Android:id= "@+id/font_14"Android:title= "@string/font_14"/> <Item Android:id= "@+id/font_16"Android:title= "@string/font_16"/> <Item Android:id= "@+id/font_18"Android:title= "@string/font_18"/> </group> </menu> </item> <!--Define a common menu item--&LT ; Item Android:id= "@+id/plain_item"android:showasaction= "Always|withtext"Android:title= "@string/plain_item" > </item> <item android:title= "@string/font_color"android:showasaction= "Always"Android:icon= "@drawable/color" > <menu> <!--define a set of allowed check menu items--<group> <!--define three menu items--<Item Android:id= "@+id/red_font"Android:title= "@string/red_title"/> <Item Android:id= "@+id/green_font"Android:title= "@string/green_title"/> <Item Android:id= "@+id/blue_font"Android:title= "@string/blue_title"/> </group> </menu> </item></menu>

<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android"        > <!--Define a set of menu items--<group android:checkablebehavior= "single" > <!--define three menu items-- <Item Android:id= "@+id/red"Android:title= "@string/red_title"Android:alphabeticshortcut= "R"/> <Item Android:id= "@+id/green"Android:title= "@string/green_title"Android:alphabeticshortcut= "G"/> <Item Android:id= "@+id/blue"Android:title= "@string/blue_title"Android:alphabeticshortcut= "B"/> </group></menu>

Interface Definition:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:orientation=" vertical "    android:layout_width=" Match_parent "     Android:layout_height= "Match_parent" >    <TextView        android:id= "@+id/txt"         Android:layout_width= "Match_parent"        android:layout_height= "Wrap_content"         Android:textsize= "15pt"        android:text= "Background color can be modified from the context menu"        /></linearlayout>

String resource:

<resources>    <string name= "app_name" >ActionItemTest</string>    <string name= "Hello_ World ">hello world!</string>    <string name=" action_settings ">Settings</string>    < String name= "Red_title" > Red </string>    <string name= "Blue_title" > Blue </string>    < String name= "Green_title" > Green </string>    <string name= "font_size" > Font size </string>    < String name= "Font_color" > Font color </string>    <string name= "Plain_item" > General menu items </string>    <string name= "font_10" >10 font </string>    <string name= "Font_12" >12 number font </string>    <string name= "font_14" >14 font </string>    <string name= "font_16" >16 number font </string>    <string name= "font_18" >18 font </string></resources>

Code:

 Packageallegro.actionbartes;ImportAndroid.graphics.Color;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.ContextMenu;ImportAndroid.view.Menu;ImportAndroid.view.MenuInflater;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsappcompatactivity {PrivateTextView txt; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); TXT=(TextView) Findviewbyid (r.id.txt); //Registering a context menu for a text boxregisterforcontextmenu (TXT); }     Public BooleanOncreateoptionsmenu (Menu menu) {menuinflater inflator=NewMenuinflater ( This); //reload the R.menu.my_menu corresponding menu and add it to the menusinflator.inflate (R.menu.menu_main, menu); return Super. Oncreateoptionsmenu (menu); }    //trigger This method when creating a context menu@Override Public voidOncreatecontextmenu (ContextMenu menu, View source, contextmenu.contextmenuinfo me Nuinfo) {Menuinflater inflator=NewMenuinflater ( This); //reload the R.menu.context corresponding menu and add it to the menusinflator.inflate (R.menu.context, menu);        Menu.setheadericon (R.drawable.tools); Menu.setheadertitle ("Please select a background color"); }    //trigger This method when the menu item in the context menu is clicked@Override Public Booleanoncontextitemselected (MenuItem mi) {//tick the menu itemMi.setchecked (true);//①        Switch(Mi.getitemid ()) { Caser.id.red:mi.setchecked (true);                Txt.setbackgroundcolor (color.red);  Break;  Caser.id.green:mi.setchecked (true);                Txt.setbackgroundcolor (Color.green);  Break;  Caser.id.blue:mi.setchecked (true);                Txt.setbackgroundcolor (Color.Blue);  Break; }        return true; } @Override//callback method After menu item is clicked     Public Booleanonoptionsitemselected (MenuItem mi) {if(Mi.ischeckable ()) {//tick the menu itemMi.setchecked (true);//②        }        //determine which menu item is clicked and respond in a targeted manner        Switch(Mi.getitemid ()) { CaseR.id.font_10:txt.settextsize (10 * 2);  Break;  CaseR.id.font_12:txt.settextsize (12 * 2);  Break;  CaseR.id.font_14:txt.settextsize (14 * 2);  Break;  CaseR.id.font_16:txt.settextsize (16 * 2);  Break;  CaseR.id.font_18:txt.settextsize (18 * 2);  Break;  CaseR.id.red_font:txt.settextcolor (color.red); Mi.setchecked (true);  Break;  CaseR.id.green_font:txt.settextcolor (Color.green); Mi.setchecked (true);  Break;  CaseR.id.blue_font:txt.settextcolor (Color.Blue); Mi.setchecked (true);  Break;  Caser.id.plain_item:toast Toast= Toast.maketext (mainactivity. This                        , "You clicked the normal menu item", Toast.length_short);                Toast.show ();  Break; }        return true; }}

Android Learning: Actionbar activity Bar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.