// 添加自定义视图
// 自定义显示控件,注意要指定其窗口参数。
private void showMessage(String name, String address) {
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.y += 20;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE// 不让其获得焦点
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE// 不让其可触摸
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;// 使其保持高亮显示
params.format = PixelFormat.TRANSLUCENT;
params.type = WindowManager.LayoutParams.TYPE_TOAST;
LayoutInflater messageInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
messageview = messageInflater.inflate(R.layout.showaddress, null);
TextView nameTextView = (TextView) messageview.findViewById(R.id.nameTextView);
TextView addressTextView = (TextView) messageview.findViewById(R.id.addressTextView);
nameTextView.setText(name);
addressTextView.setText(address);
// 利用窗口管理器来添加控件。参数:控件,控件的设置参数
windowManager.addView(messageview, params);
}
// 删除自定义视图
private void removeView() {
if (messageview != null) {
windowManager.removeView(messageview);
messageview = null;
}
}
利用WindowManager添加和删除自定义视图
最新推荐文章于 2023-01-07 10:32:52 发布