我的Android应用中有一个叠加按钮。我想在用户点击按钮时显示布局并与我的布局视图进行交互。 目前,我展示了一个祝酒词。我怎么能这样做?
这是我的OverlayShowingService.class:
public class OverlayShowingService extends Service implements OnTouchListener, OnClickListener {
private View topLeftView;
private Button overlayedButton;
private float offsetX;
private float offsetY;
private int originalXPos;
private int originalYPos;
private boolean moving;
private WindowManager wm;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
overlayedButton = new Button(this);
overlayedButton.setText("Overlay button");
overlayedButton.setOnTouchListener(this);
overlayedButton.setBackgroundColor(Color.BLACK);
overlayedButton.setOnClickListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 0;
params.y = 0;
wm.addView(overlayedButton, params);
topLeftView = new View(this);
WindowManager.LayoutParams topLeftParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
topLeftParams.gravity = Gravity.LEFT | Gravity.TOP;
topLeftParams.x = 0;
topLeftParams.y = 0;
topLeftParams.width = 0;
topLeftParams.height = 0;
wm.addView(topLeftView, topLeftParams);
}
@Override
public void onDestroy() {
super.onDestroy();
if (overlayedButton != null) {
wm.removeView(overlayedButton);
wm.removeView(topLeftView);
overlayedButton = null;
topLeftView = null;
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
float x = event.getRawX();
float y = event.getRawY();
moving = false;
int[] location = new int[2];
overlayedButton.getLocationOnScreen(location);
originalXPos = location[0];
originalYPos = location[1];
offsetX = originalXPos - x;
offsetY = originalYPos - y;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
int[] topLeftLocationOnScreen = new int[2];
topLeftView.getLocationOnScreen(topLeftLocationOnScreen);
System.out.println("topLeftY="+topLeftLocationOnScreen[1]);
System.out.println("originalY="+originalYPos);
float x = event.getRawX();
float y = event.getRawY();
WindowManager.LayoutParams params = (LayoutParams) overlayedButton.getLayoutParams();
int newX = (int) (offsetX + x);
int newY = (int) (offsetY + y);
if (Math.abs(newX - originalXPos) < 1 && Math.abs(newY - originalYPos) < 1 && !moving) {
return false;
}
params.x = newX - (topLeftLocationOnScreen[0]);
params.y = newY - (topLeftLocationOnScreen[1]);
wm.updateViewLayout(overlayedButton, params);
moving = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (moving) {
return true;
}
}
return false;
}
@Override
public void onClick(View v) {
Toast.makeText(this, "Here I want to show a layout with some options in a box", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
点击按钮后发送广播,然后从广播接收器onReceive方法开始播放活动。
创建广播接收器,然后注册服务的onCreate()广播接收器。单击按钮时的广播意图。最后,当单击叠加按钮时,接收器将打开一个活动(布局)。
如果您只想打开一个对话框,这是一个不错的答案。 https://stackoverflow.com/a/31221355/4179914
事实证明,你也可以像这里提到的对话一样主题一个活动 https://stackoverflow.com/a/7918720/4179914
public class OverlayClickReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent toDialog = new Intent(context, Main2Activity.class);
toDialog.setFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(toDialog);
}
}
点击覆盖按钮发送广播。
@Override
public void onClick(View v) {
broadcastClick();
}
private void broadcastClick() {
final Intent intent = new Intent("user.clicked.overlay.button");
final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
broadcastManager.sendBroadcast(intent);
}
注册广播接收器onCreate()
@Override
public void onCreate() {
super.onCreate();
overlayShowing = new OverlayClickReceiver();
.......
}
private void registerClickReceiver() {
LocalBroadcastManager.getInstance(this).registerReceiver(overlayShowing,
new IntentFilter("user.clicked.overlay.button"));
}
取消注册广播接收器onDestroy()
@Override
public void onDestroy() {
super.onDestroy();
..........
unregisterClickReceriver();
}
private void unregisterClickReceriver() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(overlayShowing);
}
答案 1 :(得分:0)
根据我对问题的理解,您希望从xml文件中获取视图并将其添加到窗口中。
您可以使用View view = LayoutInflater.from(context).inflate(r.layout.overlay,null,false);
并将view
与WindowManager.addView(view)
添加到窗口中。