美文网首页
【基础笔记】PopupWindow的基本使用

【基础笔记】PopupWindow的基本使用

作者: 究极无敌棒棒糖 | 来源:发表于2019-12-30 14:50 被阅读0次

    一、PopupWindow的简介

    java.lang.Object
    ↳
        android.widget.PopupWindow
    

    PopupWindow:弹出框。在activity上需要的地方显示该控件。

    二、自定义PopupWindow的使用

    1、自定义PopupWindow的布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical">
    
        <TextView
            android:id="@+id/tv_take_video"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="视频"/>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorAccent"/>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorAccent"/>
    
        <TextView
            android:id="@+id/tv_cancel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="取消"/>
    
        <TextView
            android:id="@+id/tv_take_picture"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="照片"
            />
    
    
    </LinearLayout>
    

    可以用ListView、RecyclerView,需要多写一个Item布局

    2、自定义PopupWindow继承于PopupWindow

    package com.example.administrator.popwindow;
    
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.graphics.Color;
    import android.graphics.drawable.ColorDrawable;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.WindowManager;
    import android.widget.PopupWindow;
    import android.widget.TextView;
    
    /**
     * creat by Ann
     * creat on ${DATA}
     * description
     */
    public class ChooseFilePopupwindow extends PopupWindow implements View.OnClickListener {
    
        /** 上下文 */
        private Activity mActivity;
        /** 根布局 */
        private View mRootView;
        private TextView mTvTakePicture;
        private TextView mTvTakeVideo;
        private TextView mTvCancel;
    
        private ChooseFilePopViewClickListener mChooseFilePopViewClickListener;
    
        public ChooseFilePopupwindow(Activity activity) {
            this.mActivity = activity;
            initConfig(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            initView();
        }
    
        public ChooseFilePopupwindow(Activity activity, int width, int height) {
            this.mActivity = activity;
            initConfig(width, height);
            initView();
        }
    
        @SuppressLint("NewApi")
        private void initConfig(int width, int height) {
            this.setWidth(width);
            this.setHeight(height);
            this.setFocusable(true);
            this.setTouchable(true);
            this.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            this.setOutsideTouchable(true);
            this.setElevation(25f);
            mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//达到背景全部变暗的效果
    //        mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
    //                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    //        this.setAnimationStyle(R.style.BottomPopuAnim);
            this.setOnDismissListener(new OnDismissListener() {
    
                @Override
                public void onDismiss() {
                    WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
                    mActivity.getWindow().setDimAmount(0f);
                    params.alpha = 1.0f;
                    mActivity.getWindow().setAttributes(params);
                }
            });
        }
    
        private void initView() {
            mRootView = LayoutInflater.from(mActivity).inflate(R.layout.popupwindow_choose_file, null, false);
            mTvCancel = mRootView.findViewById(R.id.tv_cancel);
            mTvTakePicture = mRootView.findViewById(R.id.tv_take_picture);
            mTvTakeVideo = mRootView.findViewById(R.id.tv_take_video);
            mTvCancel.setOnClickListener(this);
            mTvTakePicture.setOnClickListener(this);
            mTvTakeVideo.setOnClickListener(this);
            setContentView(mRootView);
        }
    
        /**
         * 显示
         * @param parent 关联父布局
         */
        public void show(View parent) {
            WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
            params.alpha = 0.7f;
            mActivity.getWindow().setDimAmount(0.7f);
            mActivity.getWindow().setAttributes(params);
            this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        }
    
    
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.tv_take_picture:
                    dismiss();
                    if(mChooseFilePopViewClickListener != null){
                        mChooseFilePopViewClickListener.takePicture();
                    }
                    break;
                case R.id.tv_take_video:
                    dismiss();
                    if(mChooseFilePopViewClickListener != null){
                        mChooseFilePopViewClickListener.takeVideo();
                    }
                    break;
                case R.id.tv_cancel:
                    dismiss();
                    break;
            }
        }
    
        public void setChooseFilePopViewClickListener(ChooseFilePopViewClickListener mChooseFilePopViewClickListener) {
            this.mChooseFilePopViewClickListener = mChooseFilePopViewClickListener;
        }
    
        public interface ChooseFilePopViewClickListener{
            public void takePicture();
            public void takeVideo();
        }
    }
    
    

    3、在activity中显示PopupWindow(可以自行选择,不适用回调方法)

    package com.example.administrator.popwindow;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
        Button button;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button = findViewById(R.id.btn_pop);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ChooseFilePopupwindow popwindow = new ChooseFilePopupwindow(MainActivity.this);
                    popwindow.show(button);
                    popwindow.setChooseFilePopViewClickListener(new ChooseFilePopupwindow.ChooseFilePopViewClickListener() {
                        @Override
                        public void takePicture() {
                            Toast.makeText(MainActivity.this,"点击了拍照",Toast.LENGTH_LONG).show();
                        }
    
                        @Override
                        public void takeVideo() {
                            Toast.makeText(MainActivity.this,"点击了摄像",Toast.LENGTH_LONG).show();
                        }
                    });
                }
            });
        }
    }
    
    
    

    总结:想好popupwindow样式进行布局---->自定义popupwindow继承于popupwindow,做好逻辑的处理(初始化,位置,大小,背景是否变暗,popupwindow的Item点击事件。)------>在activity中创建并显示。

    相关文章

      网友评论

          本文标题:【基础笔记】PopupWindow的基本使用

          本文链接:https://www.haomeiwen.com/subject/wywpoctx.html