重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

AndroidPopupWindow增加半透明蒙层

本文实例为大家分享了Android PopupWindow增加半透明蒙层的具体代码,供大家参考,具体内容如下

创新互联是一家专注于网站设计制作、网站制作与策划设计,怀仁网站建设哪家好?创新互联做网站,专注于网站建设10年,网设计领域的专业建站公司;建站业务涵盖:怀仁等地区。怀仁做网站价格咨询:18982081108

先看效果图:

Android PopupWindow增加半透明蒙层

实现代码:

BasePopupWindowWithMask.class

package com.example.popupwindowwithmask;
 
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.drawable.ColorDrawable;
import android.os.IBinder;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
 
/**
 * Created by kk on 2017/7/22.
 */
 
public abstract class BasePopupWindowWithMask extends PopupWindow {
 protected Context context;
 private WindowManager windowManager;
 private View maskView;
 
 public BasePopupWindowWithMask(Context context) {
  super(context);
  this.context = context;
  windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  setContentView(initContentView());
  setHeight(initHeight());
  setWidth(initWidth());
  setOutsideTouchable(true);
  setFocusable(true);
  setTouchable(true);
  setBackgroundDrawable(new ColorDrawable());
 }
 
 protected abstract View initContentView();
 
 protected abstract int initHeight();
 
 protected abstract int initWidth();
 
 @Override
 public void showAsDropDown(View anchor) {
  addMask(anchor.getWindowToken());
  super.showAsDropDown(anchor);
 }
 
 private void addMask(IBinder token) {
  WindowManager.LayoutParams wl = new WindowManager.LayoutParams();
  wl.width = WindowManager.LayoutParams.MATCH_PARENT;
  wl.height = WindowManager.LayoutParams.MATCH_PARENT;
  wl.format = PixelFormat.TRANSLUCENT;//不设置这个弹出框的透明遮罩显示为黑色
  wl.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;//该Type描述的是形成的窗口的层级关系
  wl.token = token;//获取当前Activity中的View中的token,来依附Activity
  maskView = new View(context);
  maskView.setBackgroundColor(0x7f000000);
  maskView.setFitsSystemWindows(false);
  maskView.setOnKeyListener(new View.OnKeyListener() {
   @Override
   public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
     removeMask();
     return true;
    }
    return false;
   }
  });
  /**
   * 通过WindowManager的addView方法创建View,产生出来的View根据WindowManager.LayoutParams属性不同,效果也就不同了。
   * 比如创建系统顶级窗口,实现悬浮窗口效果!
   */
  windowManager.addView(maskView, wl);
 }
 
 private void removeMask() {
  if (null != maskView) {
   windowManager.removeViewImmediate(maskView);
   maskView = null;
  }
 }
 
 @Override
 public void dismiss() {
  removeMask();
  super.dismiss();
 }
}

TestPopupWindow.class

package com.example.popupwindowwithmask;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
 
/**
 * Created by kk on 2017/7/22.
 */
 
public class TestPopupWindow extends BasePopupWindowWithMask {
 private int[] mIds;
 private View contentView;
 private OnItemClickListener listener;
 
 public interface OnItemClickListener {
  void OnItemClick(View v);
 }
 
 public void setOnItemClickListener(OnItemClickListener listener) {
  this.listener = listener;
 }
 
 public TestPopupWindow(Context context, int[] mIds) {
  super(context);
  this.mIds = mIds;
 
  initListener();
 }
 
 @Override
 protected View initContentView() {
  contentView = LayoutInflater.from(context).inflate(R.layout.pop_layout, null, false);
  return contentView;
 }
 
 private void initListener() {
  for (int i = 0; i < mIds.length; i++) {
   contentView.findViewById(mIds[i]).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
     if (null != listener) {
      listener.OnItemClick(v);
     }
     dismiss();
    }
   });
  }
 }
 @Override
 protected int initHeight() {
  return WindowManager.LayoutParams.WRAP_CONTENT;
 }
 @Override
 protected int initWidth() {
  return (int) (0.5 * UIUtils.getScreenWidth(context));
 }
}

MainActivity.class

package com.example.popupwindowwithmask;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 private TextView textView;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  textView = (TextView) findViewById(R.id.tv_popup);
 
 
  final TestPopupWindow testPopupWindow = new TestPopupWindow(this, new int[]{R.id.pop_location, R.id.pop_group, R.id.pop_list});
 
  textView.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    testPopupWindow.showAsDropDown(textView);
   }
  });
 
  testPopupWindow.setOnItemClickListener(new TestPopupWindow.OnItemClickListener() {
   @Override
   public void OnItemClick(View v) {
    switch (v.getId()) {
     case R.id.pop_location:
      Toast.makeText(MainActivity.this, "地址", Toast.LENGTH_SHORT).show();
      break;
     case R.id.pop_group:
      Toast.makeText(MainActivity.this, "分组", Toast.LENGTH_SHORT).show();
      break;
     case R.id.pop_list:
      Toast.makeText(MainActivity.this, "清单", Toast.LENGTH_SHORT).show();
      break;
    }
   }
  });
 }
}

pop_layout.xml

<?xml version="1.0" encoding="utf-8"?>

 
 
 
  
 
   
  
 
  
 
   
 
   
 
   
 
   
 
   
 
  
 

pop_background.xml

<?xml version="1.0" encoding="utf-8"?>

 
 

UIUtils.class

package com.example.popupwindowwithmask;
 
import android.content.Context;
 
/**
 * Created by kk on 2017/7/22.
 */
 
public class UIUtils {
 /**
  * 获得屏幕宽度
  *
  * @param context
  * @return
  */
 public static int getScreenWidth(Context context) {
  return context.getResources().getDisplayMetrics().widthPixels;
 }
 
 /**
  * 获得屏幕高度
  *
  * @param context
  * @return
  */
 public static int getScreenHeight(Context context) {
  return context.getResources().getDisplayMetrics().heightPixels;
 }
 
}

源码:下载地址

参考资料:

链接1

链接2

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


本文名称:AndroidPopupWindow增加半透明蒙层
分享URL:http://cqcxhl.com/article/johess.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP