[英]How to create a button which will stay on top of all window and can take click to open another similar overlay activity in android?
我是android编程的新手。 我已经使用系统覆盖和服务创建了一个按钮。 现在,我想要单击该按钮,并获得另一个包含5个按钮的叠加窗口。 但是问题是按钮没有任何点击,也不允许我打开另一个覆盖窗口。 这是我用来绘制按钮的xml代码-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:id="@+id/l1"
android:layout_height="fill_parent"
tools:context=".OverlayActivity" >
<Button
android:id="@+id/mainButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_vertical"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:clickable="true"
android:visibility="visible"
android:focusable="true">
</Button>
这是我用来显示叠加按钮的代码
@Override
public void onCreate() {
super.onCreate();
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View oView = layoutInflater.inflate(R.layout.activity_overlay, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
0 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(oView, params);
Button button = (Button)oView.findViewById(R.id.mainButton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(OverlayService.this, "GotCha!!! ", Toast.LENGTH_LONG).show();
}
});
ViewGroup vg = (ViewGroup)oView.findViewById(R.id.l1);
final Display metrics = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
vg.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getActionMasked()){
case MotionEvent.ACTION_MOVE:
int x = (int)event.getX() - offset_x;
int y = (int)event.getY() - offset_y;
int w = metrics.getWidth()-100;
int h = metrics.getHeight()-100;
if(x > w){
x = w;
}
if(y > h){
y = h;
}
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
lp.setMargins(x, y, 0, 0);
selected_item.setLayoutParams(lp);
break;
default:
break;
}
return true;
}
});
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getActionMasked()){
case MotionEvent.ACTION_DOWN:
offset_x = (int)event.getX();
offset_y = (int)event.getY();
selected_item = v;
break;
default:
break;
}
return false;
}
});
}
有想法吗? 我没时间了..请
最好为此使用片段
使用片段创建视图,在片段上方覆盖一个按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.