android怎么自定popupwindow设置高度无反应

author author     2023-05-12     478

关键词:

参考技术A 使用PopupWindow可实现弹出窗口效果,,其实和AlertDialog一样,也是一种对话框,两者也经常混用,但是也各有特点。下面就看看使用方法。
首先初始化一个PopupWindow,指定窗口大小参数。

PopupWindow mPop = new PopupWindow(getLayoutInflater().inflate(R.layout.window, null),
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
也可以分开写:
LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
//自定义布局
ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate(
R.layout.window, null, true);
PopupWindow mPop = new PopupWindow(menuView, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, true);
当然也可以手动设置PopupWindow大小。
mPop.setContentView(menuView );//设置包含视图
mPop.setWidth(int )
mPop.setHeight(int )//设置弹出框大小

设置进场动画:
mPop.setAnimationStyle(R.style.AnimationPreview);//设置动画样式

mPop.setOutsideTouchable(true);//这里设置显示PopuWindow之后在外面点击是否有效。如果为false的话,那么点击PopuWindow外面并不会关闭PopuWindow。当然这里很明显只能在Touchable下才能使用。

当有mPop.setFocusable(false);的时候,说明PopuWindow不能获得焦点,即使设置设置了背景不为空也不能点击外面消失,只能由dismiss()消失,但是外面的View的事件还是可以触发,back键也可以顺利dismiss掉。当设置为popuWindow.setFocusable(true);的时候,加上下面两行设置背景代码,点击外面和Back键才会消失。
mPop.setFocusable(true);
需要顺利让PopUpWindow dimiss(即点击PopuWindow之外的地方此或者back键PopuWindow会消失);PopUpWindow的背景不能为空。必须在popuWindow.showAsDropDown(v);或者其它的显示PopuWindow方法之前设置它的背景不为空:

mPop.setBackgroundDrawable(new ColorDrawable(0));

mPop.showAsDropDown(anchor, 0, 0);//设置显示PopupWindow的位置位于View的左下方,x,y表示坐标偏移量

mPop.showAtLocation(findViewById(R.id.parent), Gravity.LEFT, 0, -90);(以某个View为参考),表示弹出窗口以parent组件为参考,位于左侧,偏移-90。
mPop.setOnDismissListenerd(new PopupWindow.OnDismissListener())//设置窗口消失事件

注:window.xml为布局文件

总结:

1、为PopupWindow的view布局,通过LayoutInflator获取布局的view.如:

LayoutInflater inflater =(LayoutInflater)

this.anchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View textEntryView = inflater.inflate(R.layout.paopao_alert_dialog, null);

2、显示位置,可以有很多方式设置显示方式

pop.showAtLocation(findViewById(R.id.ll2), Gravity.LEFT, 0, -90);

或者

pop.showAsDropDown(View anchor, int xoff, int yoff)

3、进出场动画

pop.setAnimationStyle(R.style.PopupAnimation);

4、点击PopupWindow区域外部,PopupWindow消失

this.window = new PopupWindow(anchor.getContext());

this.window.setTouchInterceptor(new OnTouchListener()

@Override

public boolean onTouch(View v, MotionEvent event)

if(event.getAction() ==MotionEvent.ACTION_OUTSIDE)

BetterPopupWindow.this.window.dismiss();

return true;



return false;



);

PopupWindow 自适应宽度实例

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
int maxWidth = meathureWidthByChilds() + getPaddingLeft() + getPaddingRight(); super.onMeasure(MeasureSpec.makeMeasureSpec(maxWidth,MeasureSpec.EXACTLY),heightMeasureSpec);

public int meathureWidthByChilds()
int maxWidth = 0;
View view = null;
for (int i = 0; i < getAdapter().getCount(); i++)
view = getAdapter().getView(i, view, this);
view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
if (view.getMeasuredWidth() > maxWidth)
maxWidth = view.getMeasuredWidth();


return maxWidth;


PopupWindow自适应布局

Android自带的Menu菜单,常常无法满足我们的需求,所以就只有自己写menu菜单,通常的选择是用PopupWindow来实现自定义的menu菜单,先看代码,再来说明要注意的几点:

View menuView = inflater.inflate(R.layout.menu_popwindow, null);
final PopupWindow p = new PopupWindow(mContext);
p.setContentView(menuView);
p.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
p.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
p.setAnimationStyle(R.style.MenuWindow);
p.setOnDismissListener(this);
p.setOutsideTouchable(false);
p.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));
p.setFocusable(true); // 如果把焦点设置为false,则其他部份是可以点击的,也就是说传递事件时,不会先走PopupWindow

mPopwindow = p;本回答被提问者采纳

怎么设置android中的popupwindow进入和退出的动画

首先定义显示效果的动画文件:<?xmlversion="1.0"encoding="utf-8"?><setxmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:fromXDelta="0"android:toXDelta="0"android:fromYDelta="120"android:... 查看详情

设置自定义通话活动

...己的活动。我已经看到有很多示例,但使用的是旧版本的android,而我希望它适用于android6.0及更高版本。这意味着我必须处理权限。我设法授予了必要的权限。之后,我创建了一个继承BroadcastReceiver的类,以便我可以检测到电话... 查看详情

android怎么实现点击屏幕其他地方popupwindow消失

...tOutsideTouchable属性即可。以下为示例代码:private void showPopupWindow(View parent)           if (popupWindow == null)               LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);         ... 查看详情

android中popupwindow弹出窗体后,为啥不能点击其他控件

因为PopupWindow获得了焦点,其他得不到焦点,所以无法点击,把setFocusable设为false就行了。参考技术A设置popupwindow可点击mPopupWindow.setFocusable(true); // 设置PopupWindow可获得焦点mPopupWindow.setTouchable(true); // 设置PopupWindow... 查看详情

android怎样获取popupwindow位置

参考技术ApopupWindow.setFocusable(true);popupWindow.setOutsideTouchable(true);//这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景popupWindow.setBackgroundDrawable(newBitmapDrawable());WindowManagerwindowManager=(WindowManager)getSystemService(Context.WIN... 查看详情

android中popupwindow和dialog的区别

区别如下:1)Popupwindow在显示之前一定要设置宽高,Dialog无此限制。2)Popupwindow默认不会响应物理键盘的back,除非显示设置了popup.setFocusable(true);而在点击back的时候,Dialog会消失。3)Popupwindow不会给页面其他的部分添加蒙层,而... 查看详情

android开发,如何让popupwindow弹出时外部控件不可点击?

我这样写不行//用于PopupWindow的View2  ViewcontentView=LayoutInflater.from(context).inflate(layoutRes,null,false);3  //创建PopupWindow对象,其中:4  //第一个参数是用于PopupWindow中的View,第二个参数是PopupWindow的宽度,5  /... 查看详情

android原生popupwindow的基本使用(代码片段)

文章目录一、前言PopupWindow构造函数方法范例设置popupWindow的按键监听参考一、前言Android中的弹窗基本有两种,一种是AlertDialog,另一种是PopupWindow,AlertDialog的显示位置是固定的,PopWindow的显示位置是我们可以设... 查看详情

android原生popupwindow的基本使用(代码片段)

文章目录一、前言PopupWindow构造函数方法范例设置popupWindow的按键监听参考一、前言Android中的弹窗基本有两种,一种是AlertDialog,另一种是PopupWindow,AlertDialog的显示位置是固定的,PopWindow的显示位置是我们可以设... 查看详情

android原生popupwindow的基本使用(代码片段)

文章目录一、前言PopupWindow构造函数方法范例设置popupWindow的按键监听参考一、前言Android中的弹窗基本有两种,一种是AlertDialog,另一种是PopupWindow,AlertDialog的显示位置是固定的,PopWindow的显示位置是我们可以设... 查看详情

android:popupwindow动画显示效果

我想点击那个X按钮,在按钮上面弹出一个PopupWindow,并且以从下倒上的动画显示出来。默认的是从PW的顶部逐渐显示。我想从底部逐渐显示,该怎么设置动画。另外PW的高度和宽度如何确定。如何实现wap(刚好容纳)的高度。参... 查看详情

android弹窗探究之popupwindow的使用

参考技术A相对于AlertDialog的使用,PopupWindow的使用也比较简单,这里主要介绍的是PopupWindow的基础使用包括在使用过程中的一些注意事项,做个笔记。方式一:方式二:当然,在实际的开发过程中我们并不能仅仅满足于如何简单... 查看详情

android多个控件绑定同一个popupwindow,当点击popupwindow中的控件时获取到触发popwindow控件的文本

AndroidpopupWindow多个控件绑定同一个PopupWindow,当点击popupWindow中的控件时获取到触发popupWindow的控件的文本getText()比如说:一个界面上有三个控件: 你,我,他。 长按这三个控件中的任意一个都会弹出一个popupWindow,里面包含一... 查看详情

android中popupwindow和dialog的区别

参考技术ADialog显示的位置比较固定。PopupWindow可以通过showAtLocation设置显示位置,也可以通过ShowAsDropDown显示在某个View的相对位置 参考技术B除了外观样式和显示的位置的区别之外,他们之间最本质的区别是:dialog是非阻塞式对... 查看详情

android基础(11)popupwindow详解

参考技术A(1)PopupWindow的使用(2)自定义一个PopupWindow(3)PopupWindow的源码分析(4)AlertDialog,popupWindow,Activity区别(5)Activity-Window-View三者的差别Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:PopupWindow的位置... 查看详情

android在popupwindow里面如何切换外部的activity?

...ivity的,但是老板要求这个TabHost要隐藏,所以我就改成了PopupWindow,但是PopupWindow如何切换Activity呢?参考技术AgetActivity()方法获得Activity,然后就可以使用其startActivity等了。PopupWindow是一种不完全覆盖父窗口的View,通常一些快捷方... 查看详情

android开发设置屏蔽录制

...置可解决一般的防截屏、录屏的需求。如果页面中有弹出Popupwindow,在录屏视频中的效果是:非Popupwindow区域为黑色但Popupwindow区域仍然是可以看到的如下面两张Gif图所示:未设置FLAG_SECURE,录屏的效果,如下图(git图片中间的水... 查看详情

android中怎么让menu菜单显示在屏幕左上角

android中让菜单menu显示在左上角,可以使用popupwindow技术,也就是悬浮菜单,设置默认的位置为左上角,如下代码:package com.example.menutype;import android.app.ActionBar.LayoutParams;import android.app.Activity;import android.content.Context;import android... 查看详情