[英]How to add multiple views to window manager in Android
I am developing a application like Facebook Chat Heads a know how to add a single view to window manager. 我正在开发像Facebook Chat Heads这样的应用程序,知道如何向窗口管理器添加单个视图。
How to add multiple views to window manager? 如何向窗口管理器添加多个视图? I tried frame layout and relative layout, but how can I move chat head from one place to another place if I am using relative layout? 我尝试过框架布局和相对布局,但是如果我使用相对布局,如何将聊天头从一个地方移动到另一个地方?
For adding multiple views I used below code: 为了添加多个视图,我使用下面的代码:
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);
How can I drag chat head to remove its view? 如何拖动聊天头以删除其视图?
To add notification on top of ImageView
and move both the views together: 要在ImageView
上添加通知并将两个视图一起移动:
Use RelativeLayout
as parent Layout and add ImageView
and TextView
to it. 使用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);
Finally make these changes: 最后进行以下更改:
mWindowManager.updateViewLayout(parentlayout, params);
and 和
mWindowManager.addView(parentlayout, params);
PS: Use shapes to style TextView
and get exact notification as that of Facebook!! PS:使用形状来设置TextView
样式,并获得与Facebook相同的确切通知! :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.