Android 弹框菜单系列之王者荣耀设置PopupWindow

一.效果图

Android 弹框菜单系列之王者荣耀设置PopupWindow_第1张图片

Android 弹框菜单系列之王者荣耀设置PopupWindow_第2张图片

 

二.快速实现:

1.主函数代码:

import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.qd.douyinwu.R;
import com.example.qd.douyinwu.adapter.MyListAdapter;

import java.util.ArrayList;
import java.util.List;

public class PopupWindowWzryActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView startTime, endTime;
    private RelativeLayout rlStartTime, rlEndTime;

    private PopupWindow popupWindow;
    private ListView mlistView; //pop里面列表
    private List popData = new ArrayList<>(); //存放pop列表数据
    private ArrayAdapter popDataAdapter;
    private ImageView ivArrowR,ivArrowL;
    private boolean isShowL = false;
    private boolean isShowR = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popupwindow_wzry);
        initView();
        initPopData();
    }
    /**
     * pop里面数据
     */
    private void initPopData() {
        popData = new ArrayList<>();
        for (int i = 0; i <=11; i++) {
            popData.add(i+1+"月");
        }
    }

    private void initView() {
        startTime = (TextView) findViewById(R.id.tv_start_time);
        endTime = (TextView) findViewById(R.id.tv_end_time);
        rlStartTime = (RelativeLayout) findViewById(R.id.rl_start_time);
        rlEndTime = (RelativeLayout) findViewById(R.id.rl_end_time);
        ivArrowR = (ImageView) findViewById(R.id.iv_arrow_r);
        ivArrowL = (ImageView) findViewById(R.id.iv_arrow_l);
        rlStartTime.setOnClickListener(this);
        rlEndTime.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_start_time://开始时间
                initPop(startTime);
                if (popupWindow != null && !popupWindow.isShowing()) {
                    popupWindow.showAsDropDown(rlStartTime, 0, 0);
                    isShowL = true;
                    ivArrowL.setImageResource(R.mipmap.arrow_top);
                }else {
                    isShowL = false;
                    ivArrowL.setImageResource(R.mipmap.arrow_down);
                }
                break;
            case R.id.rl_end_time://结束时间
                initPop(endTime);
                if (popupWindow != null && !popupWindow.isShowing()) {
                    popupWindow.showAsDropDown(rlEndTime, 0, 0);
                    isShowR = true;
                    ivArrowR.setImageResource(R.mipmap.arrow_top);
                }else {
                    isShowR = false;
                    ivArrowR.setImageResource(R.mipmap.arrow_down);
                }
                break;


        }
    }

    /**
     *  初始化popupwindow
     */
    private void initPop(final TextView textView) {
        mlistView = new ListView(this);
        popDataAdapter = new ArrayAdapter<>(this, R.layout.popup_text_item, popData);
        mlistView.setAdapter(popDataAdapter);
        mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                textView.setText(popData.get(position));
                //选完之后关闭pop
                popupWindow.dismiss();
                isShowR = false;
                ivArrowR.setImageResource(R.mipmap.arrow_down);
            }
        });
        popupWindow = new PopupWindow(mlistView,rlStartTime.getWidth(), ActionBar.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                popupWindow.dismiss();
                isShowL = false;
                ivArrowL.setImageResource(R.mipmap.arrow_down);
            }
        });
        popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.shape_black));
        popupWindow.setAnimationStyle(R.style.popmenu_animation); //动画
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true); //点击pop外消失

    }
}

2.主函数布局:activity_popupwindow_wzry.xml




    

        

            

            

        

        

        

            

            

        
    

3.popup_text_item.xml



4.动画属性:popmenu_animation


    

pop_show_anim.xml




    
    

    

pop_hide_anim.xml:




    
    

    

5.背景边框:shape_black







图标请到阿里巴巴矢量图下载:https://www.iconfont.cn/

你可能感兴趣的:(弹框菜单系列,弹框)