Android with XML File Settings menu

Source: Internet
Author: User

First write the XML file under the Res/mene folder My_menu


<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >

<item
android:icon= "@drawable/ic_launcher"
android:title= "Fonts" >
<menu>

<!--define a single menu item--
<group android:checkablebehavior= "single" >
<item
Android:id= "@+id/font_10"
Android:title= "Font No. 10th"/>
<item
Android:id= "@+id/font_12"
Android:title= "Font No. 12th"/>
<item
Android:id= "@+id/font_14"
Android:title= "Font No. 14th"/>
<item
Android:id= "@+id/font_16"
Android:title= "Font No. 16th"/>
<item
Android:id= "@+id/font_18"
Android:title= "Font No. 18th"/>
</group>
</menu>
</item>
<!--define a common menu item--
<item
android:icon= "@drawable/FY_D_KF"
android:title= "normal Menu"/>
<item
android:icon= "@drawable/fy_d_kf2"
android:title= "COLOR" >
<menu>

<!--define a single menu item--
<group>
<item
Android:id= "@+id/red"
android:title= "Red"/>
<item
Android:id= "@+id/blue"
android:title= "Blue"/>
<item
Android:id= "@+id/green"
android:title= "Green"/>
</group>
</menu>
</item>

</menu>


The main code in the activity is as follows

public class Mainactivity extends Activity {

Private TextView edit;
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Menuinflater inflater = new Menuinflater (this);
Inflater.inflate (R.menu.my_menu, menu);
return Super.oncreateoptionsmenu (menu);
}

  @Override
 public boolean onoptionsitemselected (MenuItem item) {
  if ( Item.ischeckable ()) {
   item.setchecked (true);
  }
  switch (Item.getitemid ()) {
  case r.id.font_10:
    Edit.settextsize (10*2);
   break;
  case R.id.font_12:
   edit.settextsize (12*2);
   break;
  case r.id.font_14:
   edit.settextsize (14*2);
   break;
  case r.id.font_16:
   edit.settextsize (16*2);
   break;
  case r.id.font_18:
   edit.settextsize (18*2);
   break;
  }
  return true;
 }

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Edit = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Registerforcontextmenu (edit);
}
}




Android with XML File Settings menu

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.