7.25关于PopupWindow android.

本文对比了Android中PopupWindow和AlertDialog的区别,详细介绍了PopupWindow的创建、内容设置及属性配置,并提供了一个完整的示例代码,展示了如何使用PopupWindow实现一个包含ListView的弹窗。

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

PopupWindow和AlertDialog都是Android的两种对话框。

至于他们的区别呢 如下

1.AlertDialog的位置固定,而PopupWindow的位置可以随意

2.AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的

关于PopupWindow

一。

 1.创建

PopupWindow pw = new PopupWindow(view,width,height);

 2.重新设置PopupWindow的内容

pw.setContentView(popupconten);

3.设置属性

       pw.setFocusable(true);//默认是false,为false时,PopupWindow没有获得焦点能力,如果这是PopupWindow的内容中有EidtText,需要输入,这是是无法输入的;只有为true的时候,PopupWindow才具有获得焦点能力,EditText才是真正的EditText。

      pw.setAsDropDown(View view);//设置PopupWindow弹出的位置。

  pw.setOutsideTouchable(true);//这里设置显示PopuWindow之后在外面点击是否有效。如果为false的话,

//那么点击PopuWindow外面并不会关闭PopuWindow

//当然这里很明显只能在Touchable下才能使用。

下面就贴一个demo理解下吧

MainActivity.java

public class MainActivity extends Activity {

	private ImageButton ibOperationMore;

	List<Map<String, String>> moreList;
	private PopupWindow pwMyPopWindow;// popupwindow
	private ListView lvPopupList;// popupwindow中的ListView
	private int NUM_OF_VISIBLE_LIST_ROWS = 3;// 指定popupwindow中Item的数量

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		iniData();

		iniPopupWindow();

		// 更多操作按钮
		ibOperationMore = (ImageButton) findViewById(R.id.ib_operate_more);
		ibOperationMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (pwMyPopWindow.isShowing()) {

					pwMyPopWindow.dismiss();// 关闭
				} else {

					pwMyPopWindow.showAsDropDown(ibOperationMore);// 显示
				}

			}
		});
	}

	private void iniData() {

		moreList = new ArrayList<Map<String, String>>();
		Map<String, String> map;
		map = new HashMap<String, String>();
		map.put("share_key", "复制");
		moreList.add(map);
		map = new HashMap<String, String>();
		map.put("share_key", "删除");
		moreList.add(map);
		map = new HashMap<String, String>();
		map.put("share_key", "修改");
		moreList.add(map);
	}

	private void iniPopupWindow() {

		LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.task_detail_popupwindow, null);
		lvPopupList = (ListView) layout.findViewById(R.id.lv_popup_list);
		pwMyPopWindow = new PopupWindow(layout);
		pwMyPopWindow.setFocusable(true);// 加上这个popupwindow中的ListView才可以接收点击事件

		lvPopupList.setAdapter(new SimpleAdapter(MainActivity.this, moreList,
				R.layout.list_item_popupwindow, new String[] { "share_key" },
				new int[] { R.id.tv_list_item }));
		lvPopupList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Toast.makeText(MainActivity.this,
						moreList.get(position).get("share_key"),
						Toast.LENGTH_LONG).show();
			}
		});

		// 控制popupwindow的宽度和高度自适应
		lvPopupList.measure(View.MeasureSpec.UNSPECIFIED,
				View.MeasureSpec.UNSPECIFIED);
		pwMyPopWindow.setWidth(lvPopupList.getMeasuredWidth());
		pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight() + 20)
				* NUM_OF_VISIBLE_LIST_ROWS);

		// 控制popupwindow点击屏幕其他地方消失
		pwMyPopWindow.setBackgroundDrawable(this.getResources().getDrawable(
				R.drawable.bg_popupwindow));// 设置背景图片,不能在布局中设置,要通过代码来设置
		pwMyPopWindow.setOutsideTouchable(true);// 触摸popupwindow外部,popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功,如上
	}

}
activity_main.xml:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#ffffff" >

    <LinearLayout
        android:id="@+id/ll_head_bar"
        style="@style/header_linear_layout"
        android:layout_alignParentTop="true" >

        <Button
            android:id="@+id/btn_back"
            style="@style/header_button_back"
            android:layout_marginLeft="8dip" />

        <TextView
            style="@style/header_text_view"
            android:visibility="invisible" />

        <ImageButton
            android:id="@+id/ib_operate_more"
            style="@style/header_button_operate"
            android:layout_marginLeft="8dip"
            android:layout_marginRight="10dip"
            android:src="@drawable/ico_headbar_more" />
    </LinearLayout>

</RelativeLayout>
list_item_popupwindow.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/tv_list_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:minHeight="40dp"
        android:minWidth="120dp"
        android:textSize="20sp"
        android:textColor="@color/popupwindow_list_item_text_selector"
        />

</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值