自定义PopupWindow

一、自定义

public class MyPopupWindow extends PopupWindow {
   // 根视图  
    private View mRootView;  
    public View getmRootView() {
      return mRootView;
   }

   // LayoutInflater  
    LayoutInflater mInflater;
    
   public MyPopupWindow (Activity context) {
      super(context);  
        mInflater = (LayoutInflater) context  
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        mRootView = mInflater.inflate(R.layout.popwindow, null);//可以把这个布局抽出来
        setContentView(mRootView);  
  
        this.setWidth(LayoutParams.WRAP_CONTENT);  
        this.setHeight(LayoutParams.WRAP_CONTENT);  
  
        // 设置PopUpWindow弹出的相关属性  
        setTouchable(true);  
        setOutsideTouchable(true);  
        setFocusable(true);  
        setBackgroundDrawable(new BitmapDrawable(context.getResources()));  
        update();  
  
        getContentView().setFocusableInTouchMode(true);  
        getContentView().setFocusable(true);  
        setAnimationStyle(R.style.AppBaseTheme);
   }
    
}
二、布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="121dp"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:background="@drawable/pop_bg">
    
    <TextView
        android:id="@+id/tv_pop_trave"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="旅拍"
        android:textSize="18dp"
        android:textColor="@drawable/popwindow_color_selector"
        android:drawableLeft="@drawable/popwindow_travel_drawableleft_selector"
        android:drawablePadding="10dp"
        android:gravity="center"
        android:clickable="true"/>

    <TextView
        android:layout_width="110dp"
        android:layout_height="0.4dp"
        android:background="#a784a4"
        />

    <TextView
        android:id="@+id/tv_pop_honeymoon"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="蜜月"
        android:textSize="18dp"
        android:textColor="@drawable/popwindow_color_selector"
        android:drawableLeft="@drawable/popwindow_honeymoon_drawableleft_selector"
        android:drawablePadding="10dp"
        android:gravity="center"
        android:clickable="true"/>

    <TextView
        android:layout_width="110dp"
        android:layout_height="0.4dp"
        android:background="#a784a4"
        />

    <TextView
        android:id="@+id/tv_pop_wedding"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="婚礼"
        android:textSize="18dp"
        android:textColor="@drawable/popwindow_color_selector"
        android:drawableLeft="@drawable/popwindow_wedding_drawableleft_selector"
        android:drawablePadding="10dp"
        android:gravity="center"
        android:clickable="true"/>

</LinearLayout>
三、用法

private MyPopupWindow myPopupWindow;//弹出框
private View rootView;
myPopupWindow = new MyPopupWindow(this);
// 设置点击其他位置mTestPopwindow2消失
myPopupWindow.setOnDismissListener(this);//需要实现接口PopupWindow.OnDismissListener
OnMyPopwindow();
rootView = myPopupWindow.getmRootView();
//设置布局里面的监听
rootView.findViewById(R.id.tv_pop_trave).setOnClickListener(this);
/**
 * 弹出下拉窗口
 */
private void OnMyPopwindow() {
    if (myPopupWindow == null)
        return;
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    // location获得控件的位置
    int[] location = new int[2];
    View v = llPop;//llPop为要弹出框的上方控件
    if (v != null)
        v.getLocationOnScreen(location); // 控件在屏幕的位置
    myPopupWindow.setAnimationStyle(R.style.AppBaseTheme);

    // mTestPopwindow2弹出在某控件(button)的下面
    myPopupWindow.showAtLocation(v, Gravity.TOP | Gravity.LEFT, location[0]
            - v.getWidth() + CommonUtil.dipTopx(5), location[1] + v.getHeight());
    // location[1] + v.getHeight()
}
myPopupWindow.dismiss();//弹出框消失

-------------------------------------------------------------------------------

zxp的写法

/**
 * 共同的popWindow,并且将背景置透明
 *
 * @param popview 
 * @param tv
 * @param x
 */
public void showPopList (View popview, TextView tv, double x) {
    final PopupWindow mPopupWindow = new PopupWindow(popview);

    mCurrentPopupWindow = mPopupWindow;
    ViewHolder viewHolder = new ViewHolder(popview);
    mCureentViewHolder = viewHolder;

    DisplayMetrics metrics = new DisplayMetrics();
    //保存屏幕大小
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;
    int lastWidth = width;
    int lastHeight = (int) (height * x);
    mPopupWindow.setWidth(lastWidth);
    mPopupWindow.setHeight(lastHeight);
    ColorDrawable cd = new ColorDrawable(0x000000);
    mPopupWindow.setBackgroundDrawable(cd);//设置背景颜色
    //产生背景变暗效果
    WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
    lp.alpha = 0.4f;
    getActivity().getWindow().setAttributes(lp);
    mPopupWindow.setOutsideTouchable(true); //点击popupwindow外部,popupwindow也会dismiss
    mPopupWindow.setFocusable(true);
    mPopupWindow.setContentView(popview);

    //恢复背景颜色
    mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

        //在dismiss中恢复透明度
        public void onDismiss () {
            WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
            lp.alpha = 1f;
            getActivity().getWindow().setAttributes(lp);
        }
    });
    mPopupWindow.showAsDropDown(tv);

}
用法:

if (mViewProvinceCity == null) {
    mViewProvinceCity = LayoutInflater.from(v.getContext()).inflate(R.layout.popwindow_store_province_city, null);
}
showPopList(mViewProvinceCity, tvProvinceCity, 0.4);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值