Android中的Menu
Android中的设置按钮:长按或点击菜单键
1.长按选项:
布局文件:
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 <TextView 13 android:id="@+id/menutext1" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:text="第一种方法创建菜单" 17 android:textSize="20sp" /> 18 19 <TextView 20 android:id="@+id/menutext2" 21 android:layout_width="fill_parent" 22 android:layout_height="wrap_content" 23 android:text="第二种方法创建菜单" 24 android:textSize="20sp" /> 25 26 <ListView 27 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>
实现过程:
1 private ListView list; 2 private TextView lv; 3 4 String[] str = { "TextView", "EditView", "Toast(消息框)" }; 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 @Override 19 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("我是ListView"); 25 menu.add(3, 6, 0, "删除"); 26 menu.add(3, 7, 1, "修改"); 27 } 28 }); 29 // 第一种方法 30 TextView tv = (TextView) findViewById(R.id.menutext1); 31 this.registerForContextMenu(tv); 32 // 第二种方法 33 lv = (TextView) findViewById(R.id.menutext2); 34 lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { 35 36 @Override 37 public void onCreateContextMenu(ContextMenu menu, View v, 38 ContextMenuInfo menuInfo) { 39 menu.setHeaderTitle("长度-ContextMenu"); 40 menu.add(0, 1, 0, "复制"); 41 SubMenu sb = menu.addSubMenu("查找"); 42 sb.add(1, 2, 0, "按id查找"); 43 sb.add(1, 3, 0, "按名称查找"); 44 } 45 }); 46 }
2.长按菜单项:
1 private ListView list; 2 private TextView lv; 3 4 String[] str = { "TextView", "EditView", "Toast(消息框)" }; 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 @Override 19 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("我是ListView"); 25 menu.add(3, 6, 0, "删除"); 26 menu.add(3, 7, 1, "修改"); 27 } 28 }); 29 // 第一种方法 30 TextView tv = (TextView) findViewById(R.id.menutext1); 31 this.registerForContextMenu(tv); 32 // 第二种方法 33 lv = (TextView) findViewById(R.id.menutext2); 34 lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { 35 36 @Override 37 public void onCreateContextMenu(ContextMenu menu, View v, 38 ContextMenuInfo menuInfo) { 39 menu.setHeaderTitle("长度-ContextMenu"); 40 menu.add(0, 1, 0, "复制"); 41 SubMenu sb = menu.addSubMenu("查找"); 42 sb.add(1, 2, 0, "按id查找"); 43 sb.add(1, 3, 0, "按名称查找"); 44 } 45 }); 46 }
Over
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。