您的位置:首页 > 运维架构

自定义PopupWindow

2016-07-17 11:53 295 查看
一、自定义

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: