android PopupWindow 的简单使用

简单的介绍下 Android PopupWindwo的使用。因为用到了,所以做个记录。

Here we go!

Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:

  • AlertDialog的位置固定,而PopupWindow的位置可以随意
  • AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的

PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下

  • showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
  • showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
  • showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
 大体介绍了下,下面看段代码,很简单。

这是我程序的一段代码
	private void showPopupZuliaoWindow(View parent){
		Log.d(TAG2, "popZuliaoWindow ==--------");
		if (popZuliaoWindow == null) {//如果popZuliaoWindow 未实例化,执行下面代码
			Log.d(TAG2, "popZuliaoWindow!=null---init-----");
			LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);                //get 你要弹出的画面的layout文件
			View view = layoutInflater.inflate(R.layout.op_other_zuliao_speed_type, null);
                        //实例化layout中的控件
			tv_other_zuliao=(TextView)view.findViewById(R.id.tv_other_zuoliao);
                        //我在textView的左边加了一张图片,这里实例化了Drawable 的对象
			drawableZuliao= getResources().getDrawable(R.drawable.op_other_zuliao_rouhe);
                        //这里是实例化了popzuliaowindow,view 是你要弹出的画面,186和66
                         //这个view的高和宽。
			popZuliaoWindow = new PopupWindow(view,
					186, 66);
                        //设置popupzuliaowindow的背景,这句是必须要有的
			popZuliaoWindow.setBackgroundDrawable(new ColorDrawable(
					android.R.color.white));
                       //触摸屏幕的其他地方,popzuliaowindow消失
			popZuliaoWindow.setOutsideTouchable(true);
		}//这句就是显示你的popupwindow了,这里的parent 是我传入的一个button对象,所以弹出的画面就在这个
                //button的正上方-2的位置,204是屏幕上的x轴的位置。
		popZuliaoWindow.showAsDropDown(parent, -2, 204);

好了,一个简单的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="wrap_content"
    android:background="@android:color/white"
    android:orientation="horizontal" >
    <TextView 
        android:id="@+id/tv_other_zuoliao"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:drawableLeft="@drawable/op_other_zuliao_rouhe"
        android:paddingLeft="7dp"
        android:text="@string/soft"
        android:clickable="true"
        android:textColor="#000"
        android:textSize="18sp"
        />
    

</LinearLayout>




最后感谢一下  智慧云端日记 这位博友,这里的部分内容参考了他的博文。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PopupWindow是一种可以在当前界面上方显示的弹出窗口,通常用于显示一些额外的信息或者提供用户操作的选项。在Android中,可以使用PopupWindow类来创建弹出窗口。 以下是使用PopupWindow的一般步骤: 1. 创建PopupWindow对象:使用PopupWindow的构造函数创建一个PopupWindow对象。 2. 设置PopupWindow的属性:设置PopupWindow的大小、位置、背景等属性。 3. 设置PopupWindow的内容视图:使用setContentView方法设置PopupWindow的内容视图,这可以是一个布局文件或者一个View对象。 4. 显示PopupWindow使用showAsDropDown、showAtLocation等方法显示PopupWindow。 5. 处理PopupWindow的事件:设置PopupWindow的监听器,处理PopupWindow的各种事件。 以下是一个简单的例子,展示如何使用PopupWindow: ``` // 创建PopupWindow对象 PopupWindow popupWindow = new PopupWindow(context); // 设置PopupWindow的属性 popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setFocusable(true); // 设置PopupWindow的内容视图 View contentView = LayoutInflater.from(context).inflate(R.layout.popup_layout, null); popupWindow.setContentView(contentView); // 显示PopupWindow popupWindow.showAsDropDown(anchorView); // 处理PopupWindow的事件 contentView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理点击事件 popupWindow.dismiss(); } }); ``` 在上面的代码中,我们创建了一个PopupWindow对象,并设置了宽高、背景等属性。然后,我们使用LayoutInflater加载了一个布局文件作为PopupWindow的内容视图,并使用setContentView方法设置了PopupWindow的内容视图。最后,我们使用showAsDropDown方法显示了PopupWindow,并设置了一个点击事件处理器来处理用户点击PopupWindow的事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值