写一个悬浮在activity上的dialog,在dialog没有覆盖的地方,不影响activity的使用
效果图:
代码很简单:
/**
* @ 类名:
* Created by StoneWay on 2018/6/21.
* @ 包涵:
*/
public class DialogoneActivityextends Activityimplements View.OnClickListener {
private Dialogdialog;
private TextViewtvshow;
private int clickcounts =1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialogone);
Button bt_dialog = (Button) findViewById(R.id.bt_dialog);
bt_dialog.setOnClickListener(this);
Button bt_dialog1 = (Button) findViewById(R.id.bt_dialog1);
bt_dialog1.setOnClickListener(this);
tvshow = (TextView) findViewById(R.id.tv_show);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_dialog:
if (dialog ==null) {
showDialog();
}else {
dialog.dismiss();
showDialog();
}
break;
case R.id.bt_dialog1:
clickcounts +=1;
Toast.makeText(DialogoneActivity.this, "+" +clickcounts, Toast.LENGTH_SHORT).show();
tvshow.setText("已经点击" +clickcounts);
break;
default:
break;
}
}
private void showDialog() {
dialog =new Dialog(this);
dialog.setContentView(R.layout.dialog_layout);
dialog.setTitle("dialog");
Window window =dialog.getWindow();
android.view.WindowManager.LayoutParams params = window.getAttributes();
params.x =100; // 新位置X坐标
params.y =100; // 新位置Y坐标
params.width =700; // 宽度
params.height =700; // 高度
params.alpha =0.7f; // 透明度
params.gravity = Gravity.TOP;
params.dimAmount =0.0f; // 去背景遮盖
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
//params.dimAmount = 1.0f; // 去背景遮盖
window.setGravity(Gravity.LEFT | Gravity.TOP);
window.setAttributes(params);
dialog.show();
}
}