第一个:popupWindow
//创建一个pop实例化对象 popupWindow = new PopupWindow(); //设置pop的view视图 View view=View.inflate(this,R.layout.foot,null); popupWindow.setContentView(view); //给pop设置宽和高 popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); //设置pop外面点击消失 popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new ColorDrawable()); //给pop的菜单设置点击事件 TextView textView=view.findViewById(R.id.title1); TextView textView1=view.findViewById(R.id.title2); textView.setOnClickListener(this); textView.setOnClickListener(this);
第二个:1.通知栏 notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); Intent intent=new Intent(this,Main2Activity.class); PendingIntent pendingIntent=PendingIntent.getActivity(this,1,intent,PendingIntent.FLAG_CANCEL_CURRENT); Notification notification= new Notification.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("QQ") .setContentText("妞子发来请求加我好友") .setContentIntent(pendingIntent) .build(); notificationManager.notify(1,notification); 2.第2中方法 //1 获取manager 通知管理者对象 //3在这里创建一个通知对象 这个是代码分解开来写了,这里面添加了一个点击通知跳转到activity的功能 Notification.Builder builder=new Notification.Builder(this); builder.setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("qq") .setContentText("张柏芝请求加你好友"); //4跳转的功能使用了之前我们用到的intent来跳转 Intent intent=new Intent(this,Main2Activity.class); //5但是通知有自己的intent来进行跳转 它自己的intent叫做 pendingIntent //pendingIntent 不能直接new 需要调换用getActivity来获取 里面传递的参数 //1 上下文对象 2 请求码 一般给1 3 intent对像 4 一个tag值 我们一般固定使用PendingIntent.FLAG_UPDATE_CURRENT(更新activity) PendingIntent pendingIntent=PendingIntent.getActivity(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT); // 把pendingIntet添加到builder对象中 builder.setContentIntent(pendingIntent); //通过builder的build方法来获取通知 Notification notifaction=builder.build();
第三个:菜单栏
public class MainActivity extends AppCompatActivity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { tv = (TextView) findViewById(R.id.tv); this.registerForContextMenu(tv); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.add(1,1,1,"我是一只松鼠"); menu.add(1,2,3,"我是一只傻松鼠"); menu.add(1,3,5,"我是一只二松鼠"); menu.add(1,4,4,"我是一只公松鼠"); menu.add(1,5,2,"我是一只懒松鼠"); super.onCreateContextMenu(menu, v, menuInfo); } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: Toast.makeText(this,"我是一只耗子",Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(this,"我是一只耗子",Toast.LENGTH_SHORT).show(); break; case 3: Toast.makeText(this,"我是一只耗子",Toast.LENGTH_SHORT).show(); break; case 4: Toast.makeText(this,"我是一只耗子",Toast.LENGTH_SHORT).show(); break; case 5: Toast.makeText(this,"我是一只耗子",Toast.LENGTH_SHORT).show(); break; } return true; } }