我正在开发像Facebook Chat Heads这样的应用程序,知道如何向窗口管理器添加单个视图。
如何向窗口管理器添加多个视图?我尝试了框架布局和相对布局,但如果我使用相对布局,如何将聊天头从一个地方移动到另一个地方?
为了添加多个视图,我在下面使用了代码:
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);
如何拖动聊天头以删除其视图?
答案 0 :(得分:9)
要在ImageView
之上添加通知并将两个视图一起移动:
使用RelativeLayout
作为父版面,并为其添加ImageView
和TextView
。
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相同的确切通知! :)