1、定义pop
<pre name="code" class="java">private PopupWindow citypop;
private TextView tv_province,tv_scity,tv_area,tv_ok;
private TextView tv_city;//父布局中的一个控件
2、创建pop
/**
* 选择省-市-县
* @Title: citypop
* @Description: TODO
* @return void
* @author
* @date 2015-10-14 下午5:20:54
* @throws
*/
private void citypop() {
if (null == citypop) {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View pv = layoutInflater.inflate(R.layout.pop_weather_select,
null);
tv_province = (TextView) pv.findViewById(R.id.tv_province);
tv_scity = (TextView) pv.findViewById(R.id.tv_scity);
tv_area = (TextView) pv.findViewById(R.id.tv_area);
tv_ok = (TextView) pv.findViewById(R.id.tv_ok);
tv_province.setOnClickListener(this);
tv_scity.setOnClickListener(this);
tv_area.setOnClickListener(this);
tv_ok.setOnClickListener(this);
citypop = new PopupWindow(pv,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
citypop.setAnimationStyle(R.anim.push_bottom_in);
citypop.setTouchable(true);
citypop.setFocusable(true);// 使其聚集
citypop.setOutsideTouchable(true);//设置允许在外点击消失
}
WindowManager.LayoutParams params = this.getWindow()
.getAttributes();
params.alpha = 0.7f;
this.getWindow().setAttributes(params);
// 添加pop窗口关闭事件
citypop.setOnDismissListener(new poponDismissListener());
citypop.showAtLocation(this.tv_city, Gravity.CENTER | Gravity.CENTER, 0, 0);
}
/**
*
* @ClassName: poponDismissListener
* @Description: 点击屏幕外消失
* @author
* @date 2015-10-14 下午5:28:28
*
*/
class poponDismissListener implements PopupWindow.OnDismissListener {
@Override
public void onDismiss() {
backgroundAlpha(1f);
}
}
/**
* 设置添加屏幕的背景透明度
* @Title: backgroundAlpha
* @Description: TODO
* @param bgAlpha
* @return void
* @author
* @date 2015-10-14 下午5:28:50
* @throws
*/
public void backgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = this.getWindow()
.getAttributes();
lp.alpha = bgAlpha;
this.getWindow().setAttributes(lp);
}
3、布局pop_weather_select.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_margin="5dp" >
<TextView
android:id="@+id/tv_province"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_weight="1"
android:gravity="center"
android:layout_margin="2dp"
android:background="@drawable/app_corner_textview"
android:textColor="@color/color_white"
android:text="选择省份"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_scity"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/app_corner_textview"
android:gravity="center"
android:text="选择城市"
android:textColor="@color/color_white"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_area"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_weight="1"
android:gravity="center"
android:layout_margin="2dp"
android:background="@drawable/app_corner_textview"
android:textColor="@color/color_white"
android:text="选择地区"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_ok"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_weight="1"
android:gravity="center"
android:layout_margin="2dp"
android:background="@drawable/app_corner_textview"
android:textColor="@color/color_white"
android:text="确定"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>