PopupWindow

本文介绍如何使用Android自定义PopupWindow实现省市区选择功能。通过LayoutInflater加载布局,并为PopupWindow设置触摸、聚焦及动画效果。同时,实现了点击屏幕外部区域时PopupWindow自动关闭的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>




### Android PopupWindow 使用教程 #### 创建并展示PopupWindow实例 在Android开发中,`PopupWindow` 是一种灵活的方式,在当前Activity之上显示临时性的浮层视图。要创建一个基本的 `PopupWindow`, 需先准备布局文件作为弹出窗口的内容[^1]。 假设有一个名为 `popup_layout.xml` 的布局资源文件: ```xml <!-- popup_layout.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/popup_element" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:text="This is a Popup Window!" android:padding="20dp" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> ``` 接着是在Java代码里初始化和配置这个 `PopupWindow` 对象: ```java // 获取LayoutInflater服务来加载布局 ViewGroup container = findViewById(R.id.container); View customView = getLayoutInflater().inflate(R.layout.popup_layout, null); // 初始化PopupWindow对象 PopupWindow popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); // 设置可点击外部区域关闭PopupWindow popupWindow.setOutsideTouchable(true); // 如果希望背景变暗可以启用下面这行代码 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 显示PopupWindow于指定位置下方 popupWindow.showAsDropDown(container); ``` 上述例子展示了如何通过给定容器(`container`)底部的位置来呈现 `PopupWindow`. 此外还有其他方式如 `showAtLocation()` 可以更精确控制其出现地点[^3]. 对于构造函数的选择上,除了最基础的形式之外,还可以传递更多参数来自定义行为,比如宽度高度、焦点属性等[^2]: ```java PopupWindow(Context context) PopupWindow(int width, int height) PopupWindow(View contentView, int width, int height) ... ``` 以上就是关于 `PopupWindow` 基本使用的介绍以及简单案例说明.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值