当前位置: 移动技术网 > 科技>操作系统>windows > Android中自定义PopupWindow

Android中自定义PopupWindow

2020年08月14日  | 移动技术网科技  | 我要评论
public class SelectPicPopupWindow extends PopupWindow { //指定PopupWindow的宽高 public SelectPicPopupWindow(Context context,int width,int height) {super(context);this.context = context; //PopupWindow中显示的自定义xml布局LayoutInflater inflater.
public class SelectPicPopupWindow extends PopupWindow {

    //指定PopupWindow的宽高
    public SelectPicPopupWindow(Context context,int width,int height) {
		super(context);
		this.context = context;
        //PopupWindow中显示的自定义xml布局
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMenuView = inflater.inflate(R.layout.layout_popupwindow, null);
        //此例显示的是list列表
		smlv_manage_list = mMenuView.findViewById(R.id.smlv_manage_list);
		smlv_manage_list.setChoose(true);
		drawBeanList = new ArrayList<>();
		drawingAdapter = new DrawingAdapter(context,drawBeanList);
		smlv_manage_list.setAdapter(drawingAdapter);
		// 设置按钮监听
		smlv_manage_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
				drawingListener.setOnItemClickListener(view,drawBeanList.get(position));
			}
		});

		
		// 设置SelectPicPopupWindow的View
		this.setContentView(mMenuView);
		// 设置SelectPicPopupWindow弹出窗体的宽
        if (width==0){
            width = LayoutParams.MATCH_PARENT;
        }
		this.setWidth(width);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(height);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.PopupAnimation);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0x00000000);
		// 设置SelectPicPopupWindow弹出窗体的背景
		this.setBackgroundDrawable(dw);
		// mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
		mMenuView.setOnTouchListener(new OnTouchListener() {
			@Override
			@SuppressLint("ClickableViewAccessibility")
			public boolean onTouch(View v, MotionEvent event) {
				int height = mMenuView.findViewById(R.id.popupwindow).getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
					}
				}
				return true;
			}
		});
		initVisualFileLv();
	}
}
<!--  选择照片底部弹出层  -->
<style name="PopupAnimation" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
        <item name="android:windowExitAnimation">@anim/push_bottom_out</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- push_bottom_in -->
    <translate
        android:duration="200"
        android:fromYDelta="100%p"
        android:toYDelta="0"        
     />   
     <alpha
	android:fromAlpha="0.0"
	android:toAlpha="1.0"
	android:duration="200"
	/>     
</set>
<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- push_bottom_out -->
    <translate
        android:duration="200"
        android:fromYDelta="0"
        android:toYDelta="50%p" />
 <alpha
	android:fromAlpha="1.0"
	android:toAlpha="0.0"
	android:duration="200"
	/>  
</set>

使用:(包含PopupWindow背景灰色效果,参考网址:https://blog.csdn.net/pang9998/article/details/88738838

private void showPicturePopupWindow(){
        //获取屏幕的宽高
        int width = getWindowManager().getDefaultDisplay().getWidth();
        width = width*2/3;
        int height = getWindowManager().getDefaultDisplay().getHeight();
        height = height*4/5;
        menuWindow = new SelectPicPopupWindow(this,width,height);
        menuWindow.setDrawingListener(new SelectPicPopupWindow.DrawingOnClickListener() {
            @Override
            public void setOnItemClickListener(View view, DrawBean drawingBean) {
                showToast("点击了条目....");
            }
        });
        darkenBackground(0.5f);
        //父控件ID
        //显示的位置CENTER居中显示
        //x、y指的是偏移量坐标点
        menuWindow.showAtLocation(findViewById(R.id.activity_main),
                Gravity.CENTER, 0, 0);//Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL
        menuWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                darkenBackground(1f);
            }
        });
    }
//改变背景颜色
private void darkenBackground(Float bgcolor){
        //背景灰色效果
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgcolor;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getWindow().setAttributes(lp);
}

 

本文地址:https://blog.csdn.net/aidou1314/article/details/107951447

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网