Android Menu and AndroidMenu

Source: Internet
Author: User

Android Menu and AndroidMenu

Set button in Android: Long press or click the menu key

1. Long press options:

Layout file:

1 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical" 6 android: paddingBottom = "@ dimen/activity_vertical_margin" 7 android: paddingLeft = "@ dimen/activity_horizontal_margin" 8 android: paddingRight = "@ dimen/activity_horizontal_margin" 9 android: paddingTop = "@ dimen/activity_vertical_margin" 10 tools: context = ". menuActivity "> 11 12 <TextView13 android: id =" @ + id/menutext1 "14 android: layout_width =" fill_parent "15 android: layout_height =" wrap_content "16 android: text = "Method 1: Create a menu" 17 android: textSize = "20sp"/> 18 19 <TextView20 android: id = "@ + id/menutext2" 21 android: layout_width = "fill_parent" 22 android: layout_height = "wrap_content" 23 android: text = "method 2" 24 android: textSize = "20sp"/> 25 26 <ListView27 android: id = "@ + id/menulist" 28 android: layout_width = "fill_parent" 29 android: layout_height = "fill_parent" 30 android: layout_marginTop = "50dp"> 31 </ListView> 32 33 </LinearLayout>

Implementation process:

1 private ListView list; 2 private TextView lv; 3 4 String [] str = {"TextView", "EditView", "Toast (message box )"}; 5 6 @ Override 7 protected void onCreate (Bundle savedInstanceState) {8 super. onCreate (savedInstanceState); 9 setContentView (R. layout. activity_menu); 10 11 list = (ListView) findViewById (R. id. menulist); 12 ArrayAdapter <String> arr = new ArrayAdapter <String> (this, 13 android. r. layout. simple_list_item_1, str); 14 list. setAdapter (arr); 15 16 list. setOnCreateContextMenuListener (new OnCreateContextMenuListener () {17 18 @ Override19 public void onCreateContextMenu (ContextMenu menu, View v, 20 ContextMenuInfo menuInfo) {21 menu. clear (); 22 menu. clearHeader (); 23 menu. setHeaderIcon (R. drawable. ic_launcher); 24 menu. setHeaderTitle ("My name is ListView"); 25 menu. add (3, 6, 0, "delete"); 26 menu. add (3, 7, 1, "modify"); 27} 28}); 29 // Method 30 TextView TV = (TextView) findViewById (R. id. menutext1); 31 this. registerForContextMenu (TV); 32 // Method 33 lv = (TextView) findViewById (R. id. menutext2); 34 lv. setOnCreateContextMenuListener (new OnCreateContextMenuListener () {35 36 @ Override37 public void onCreateContextMenu (ContextMenu menu, View v, 38 ContextMenuInfo menuInfo) {39 menu. setHeaderTitle ("length-ContextMenu"); 40 menu. add (0, 1, 0, "copy"); 41 SubMenu sb = menu. addSubMenu ("Search"); 42 sb. add (1, 2, 0, "search by id"); 43 sb. add (1, 3, 0, "search by name"); 44} 45}); 46}

2. Long press menu item:

1 private ListView list; 2 private TextView lv; 3 4 String [] str = {"TextView", "EditView", "Toast (message box )"}; 5 6 @ Override 7 protected void onCreate (Bundle savedInstanceState) {8 super. onCreate (savedInstanceState); 9 setContentView (R. layout. activity_menu); 10 11 list = (ListView) findViewById (R. id. menulist); 12 ArrayAdapter <String> arr = new ArrayAdapter <String> (this, 13 android. r. layout. simple_list_item_1, str); 14 list. setAdapter (arr); 15 16 list. setOnCreateContextMenuListener (new OnCreateContextMenuListener () {17 18 @ Override19 public void onCreateContextMenu (ContextMenu menu, View v, 20 ContextMenuInfo menuInfo) {21 menu. clear (); 22 menu. clearHeader (); 23 menu. setHeaderIcon (R. drawable. ic_launcher); 24 menu. setHeaderTitle ("My name is ListView"); 25 menu. add (3, 6, 0, "delete"); 26 menu. add (3, 7, 1, "modify"); 27} 28}); 29 // Method 30 TextView TV = (TextView) findViewById (R. id. menutext1); 31 this. registerForContextMenu (TV); 32 // Method 33 lv = (TextView) findViewById (R. id. menutext2); 34 lv. setOnCreateContextMenuListener (new OnCreateContextMenuListener () {35 36 @ Override37 public void onCreateContextMenu (ContextMenu menu, View v, 38 ContextMenuInfo menuInfo) {39 menu. setHeaderTitle ("length-ContextMenu"); 40 menu. add (0, 1, 0, "copy"); 41 SubMenu sb = menu. addSubMenu ("Search"); 42 sb. add (1, 2, 0, "search by id"); 43 sb. add (1, 3, 0, "search by name"); 44} 45}); 46}

Over

Tags: Android, 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.