android 为组件添加contextMenu上下文菜单
package com.example.fragmentNavigation2.fragment; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.*; import android.widget.Button; import android.widget.Toast; import com.example.fragmentNavigation2.R; public class WeiXinFragment extends Fragment { private Button button; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.wei_xin_fragment, container, false); button = (Button) view.findViewById(R.id.press_button); registerForContextMenu(button); return view; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { new MenuInflater(getActivity()).inflate(R.menu.context_menu, menu); menu.setHeaderTitle("请选择你喜欢的网站"); super.onCreateContextMenu(menu, v, menuInfo); } /** * 相应本fragment对应的上下文菜单事件 */ @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.first_item: Toast.makeText(getActivity(), "重庆国际矿产开发协会", 1).show(); break; case R.id.second_item: Toast.makeText(getActivity(), "重庆雨人网络工作室", 1).show(); break; } return true; } }
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/first_item" android:title="第一个选项"></item> <item android:id="@+id/second_item" android:title="第二个选项"></item> <item android:title="第三个选项"></item> </menu>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。