Dialog(九)——利用WindowManager在屏幕任意位置添加Dialog
2020-12-13 02:34
标签:android c class blog code java Dialog(九)——利用WindowManager在屏幕任意位置添加Dialog,搜素材,soscw.com Dialog(九)——利用WindowManager在屏幕任意位置添加Dialog 标签:android c class blog code java 原文地址:http://blog.csdn.net/lfdfhl/article/details/28888921package cn.testalertdialog;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
/**
* Demo描述:
* 利用WindowManager在屏幕任意位置添加和移除"自定义对话框"
* 本质上是使用了WindowManager在屏幕上添加了View,只是
* 我们把这个View做成了常见的对话框的样式
*
* 参考资料:
* http://blog.csdn.net/lfdfhl/article/details/8195466
*
*/
public class MainActivity extends Activity {
private View mDialogView;
private WindowManager mWindowManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
mWindowManager = getWindowManager();
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
mDialogView = inflater.inflate(R.layout.dialogview, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.y += 100;
params.x += -30;
initDialogComponents(mDialogView);
//添加对话框
mWindowManager.addView(mDialogView, params);
}
//初始化自定义对话框中的控件
private void initDialogComponents(View dialogView){
Button firstButton=(Button) dialogView.findViewById(R.id.dialog_firstButton);
firstButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("点击了第一个Button");
}
});
Button secondButton=(Button) dialogView.findViewById(R.id.dialog_secondButton);
secondButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mDialogView!=null) {
//移除对话框
mWindowManager.removeView(mDialogView);
}
}
});
}
}
main.xml如下:
dialogview.xml如下:
文章标题:Dialog(九)——利用WindowManager在屏幕任意位置添加Dialog
文章链接:http://soscw.com/essay/25874.html