如何在Android中添加多个视图到窗口pipe理器

Android截图

我正在开发一个应用程序,如Facebook聊天头 ,知道如何添加一个单一的视图窗口pipe理器。

如何将多个视图添加到窗口pipe理器? 我尝试了框架布局和相对布局,但是如果我使用相对布局,如何将聊天头从一个地方移动到另一个地方?

为了添加多个视图我使用下面的代码:

chatHead = new ImageView(this); chatHead.setImageResource(R.drawable.ic_launcher); TextView t = new TextView(this); t.setText("Blessan Mathew"); t.setBackgroundColor(Color.CYAN); params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); childLayout.addView(t, params1); params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); childLayout.addView(chatHead, params1); fr.addView(childLayout); params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT ); params.gravity = Gravity.TOP | Gravity.LEFT; params.x = 0; params.y = 100; windowManager.addView(fr, params); 

我怎样才能拖动聊天头删除其视图?

Solutions Collecting From Web of "如何在Android中添加多个视图到窗口pipe理器"

要在ImageView上添加通知并将两个视图一起移动:

使用RelativeLayout作为父Layout,并将ImageViewTextView添加到它。

 private RelativeLayout parentlayout; TextView notification; chatHead = new ImageView(this); chatHead.setImageResource(R.drawable.deals); chatHead.setId(imageid); parentlayout = new RelativeLayout(this); notification = new TextView(this); notification.setTextColor(Color.parseColor("#494949")); notification.setText("1"); notification.setId(nameid); notification.setTextSize(19); final RelativeLayout.LayoutParams params_imageview = new RelativeLayout.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT); params_imageview.addRule(RelativeLayout.ALIGN_PARENT_LEFT); final RelativeLayout.LayoutParams params_name = new RelativeLayout.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT); params_name.addRule(RelativeLayout.ALIGN_RIGHT, imageid); params_name.addRule(RelativeLayout.ALIGN_TOP, imageid); parentlayout.addView(chatHead, params_imageview);// adding user image to view parentlayout.addView(notification, params_name); 

最后做出这些改变:

 mWindowManager.updateViewLayout(parentlayout, params); 

 mWindowManager.addView(parentlayout, params); 

PS:使用形状来设置TextView风格,并获得与Facebook相同的确切通知! 🙂