android的popupwindow的使用

android的popupwindow常用形式


一般android用到的pop弹窗

直接上代码

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

/**
 * Created by xieshengqi on 16/11/12.
 */
public class PopListViewWindow extends PopupWindow {


    private ListView listView;
    private TextView textView;
    private ImageView close_image;

    public PopListViewWindow(Context context) {
        super(context);
        initView(context);
    }

    public PopListViewWindow(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public PopListViewWindow(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }


    private void initView(final Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.pop_listview_layout, null);
        listView = (ListView) view.findViewById(R.id.pop_listview_listView);
        textView = (TextView) view.findViewById(R.id.pop_listView_title_textView);
        close_image = (ImageView) view.findViewById(R.id.pop_addqingdan_cancle);
        LinearLayout.LayoutParams pa = (LinearLayout.LayoutParams) listView.getLayoutParams();
        pa.height = DisplayUtil.getScreenHeight() / 2;
        listView.setLayoutParams(pa);
        setContentView(view);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 实例化一个ColorDrawable颜色为半透明
        final ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOutsideTouchable(true);
        close_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

点击pop窗口之外的地方隐藏窗口可以实现监听

view.setOnTouchListener(new View.OnTouchListener() {

           public boolean onTouch(View v, MotionEvent event) {
              int height = view.findViewById(
                        R.id.pop_choosecuoti_uselayout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y > height) {
                        dismiss();
                   }
               }
               return true;
           }
        });
height为窗口实际的控件高度。


这是一般常用的一个pop弹窗





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值