PopupWindow的属性及用法

PopupWindow可实现弹窗效果,但是,Popupwidow没有默认布局,需要我们自己设置。
一.PopupWindow的API
构造方法摘要
PopupWindow() 
          Create a new empty, non focusable popup window of dimension (0,0).
 //创建一个空的、没有获取焦点的、长宽尺寸均为0的popup window
PopupWindow(Context context) 
          Create a new empty, non focusable popup window of dimension (0,0).
 //创建一个空的、没有获取焦点的、长宽尺寸均为0的popup window
PopupWindow(Context context, AttributeSet attrs) 
          Create a new empty, non focusable popup window of dimension (0,0).
 //创建一个空的、没有获取焦点的、长宽尺寸均为0的popup window
PopupWindow(Context context, AttributeSet attrs, int defStyle) 
          Create a new empty, non focusable popup window of dimension (0,0).
 //创建一个空的、没有获取焦点的、长宽尺寸均为0的popup window
PopupWindow(int width, int height) 
          Create a new empty, non focusable popup window.
 //创建一个没有获取焦点的、宽为width长为height的popup window
PopupWindow(View contentView) 
          Create a new non focusable popup window which can display the contentView.
 //创建一个没有获取焦点的、显示contentView的popup window
PopupWindow(View contentView, int width, int height) 
          Create a new non focusable popup window which can display the contentView.
 //创建一个没有获取焦点的、宽为width长为height显示contentView的popup window
PopupWindow(View contentView, int width, int height, boolean focusable) 
          Create a new popup window which can display the contentView.
 //创建一个根据focusable来判断是否获取焦点的、宽为width长为height显示contentView的popup window
方法摘要
 void dismiss() 
          Dispose of the popup window.
 //使popup window消失
 int getAnimationStyle() 
          Return the animation style to use the popup appears and disappears
 //返回该popup window出现或消失的动画类型,返回值为整型
 Drawable getBackground() 
          Return the drawable used as the popup window's background.
 //返回popup window的背景,返回值为Drawable类型
 View getContentView() 
          Return the view used as the content of the popup window.
 //返回popup window展示的内容,返回值为一个View
 int getHeight() 
          Return this popup's height MeasureSpec
 //返回该popup window的高度值,返回值为int型
 int getInputMethodMode() 
          Return the current value in setInputMethodMode(int).
 //返回当前popup window对输入法的设置模式
 int getMaxAvailableHeight(View anchor) 
          Returns the maximum height that is available for the popup to be completely shown.
 //返回能使当前popup window完整显示的最大高度
 int getMaxAvailableHeight(View anchor, int yOffset) 
          Returns the maximum height that is available for the popup to be completely shown.
 //返回能使当前popup window完整显示的最大高度
 int getWidth() 
          Return this popup's width MeasureSpec
 //返回该popup window的宽度值,返回值为int型
 boolean isAboveAnchor() 
          Indicates whether the popup is showing above (the y coordinate of the popup's bottom is less than the y coordinate of the anchor) 
 or below the anchor view (the y coordinate of the popup is greater than y coordinate of the anchor's bottom).
 //判断popup window是否超出了父View的布局之外
 boolean isClippingEnabled() 
          Indicates whether clipping of the popup window is enabled.
 boolean isFocusable() 
          Indicate whether the popup window can grab the focus.
 //判断当前popup window是否获得焦点
 boolean isOutsideTouchable() 
          Indicates whether the popup window will be informed of touch events outside of its window.
 //判断当前popup window的外界触碰是否有响应
 boolean isShowing() 
          Indicate whether this popup window is showing on screen.
 //判断当前popup window是否处于显示状态
 boolean isTouchable() 
          Indicates whether the popup window receives touch events.
 //判断当前popup window是否接受触摸事件
 void setAnimationStyle(int animationStyle) 
          Change the animation style resource for this popup.
 //设置popup window的动画样式,有什么样式去Animation
 void setBackgroundDrawable(Drawable background) 
          Change the background drawable for this popup window.
 //设置popup window背景
 void setClippingEnabled(boolean enabled) 
          Allows the popup window to extend beyond the bounds of the screen.
 /*允许弹出窗口超出屏幕范围。默认情况下,窗口被夹到屏幕边界。设置为false将允许Windows精确定位。
 也就是说,如果这个Popupwindow显示区域 不足或者层叠将导致绘制位置计算不准确,需要手动设置为false */
 void setContentView(View contentView) 
          Change the popup's content.
 //设置该popup window显示的内容
 void setFocusable(boolean focusable) 
          Changes the focusability of the popup window.
 //设置该popup window是否获得焦点
 void setHeight(int height) 
          Change the popup's height MeasureSpec If the popup is showing, calling this method will take effect only the next time the popup is shown.
 void setIgnoreCheekPress() 
          Set the flag on popup to ignore cheek press eventt; by default this flag is set to false which means the pop wont ignore cheek press dispatch events.
// 设置是否忽略“脸颊触碰”,默认为false,即不忽略。
什么是“脸颊触碰”? Events都有一个大小,当Events的大小比手指尺寸大时即为CheekPress,这个可能常用于打电话时脸颊碰到屏幕的情况*/
 void setInputMethodMode(int mode) 
          Control how the popup operates with an input method: one of INPUT_METHOD_FROM_FOCUSABLE, INPUT_METHOD_NEEDED, or INPUT_METHOD_NOT_NEEDED.
 /*设置输入法的模式,参数为这三个:
 INPUT_METHOD_FROM_FOCUSABLE(根据是否可以获得焦点决定), 
 INPUT_METHOD_NEEDED(允许输入法), 
 INPUT_METHOD_NOT_NEEDED(不允许输入法)。
 当popup window中包含EditText时可能会涉及这方面,可能是出现输入法显示在popup window后面,导致无法输入的问题,可以修改焦点和这里。*/

 void setOnDismissListener(PopupWindow.OnDismissListener onDismissListener) 
          Sets the listener to be called when the window is dismissed.
 //popup window消失时的监听
 void setOutsideTouchable(boolean touchable) 
          Controls whether the pop-up will be informed of touch events outside of its window.
 //控制popup window以外的区域被点击后是否有影响,一般与setTouchInterceptor配合使用
 void setTouchable(boolean touchable) 
          Changes the touchability of the popup window.
 //设置popup window是否可触碰
 void setTouchInterceptor(View.OnTouchListener l) 
          Set a callback for all touch events being dispatched to the popup window.
 //当popup window被触碰时的回调方法
 void setWidth(int width) 
          Change the popup's width MeasureSpec If the popup is showing, calling this method will take effect only the next time the popup is shown.
 //修改popup window的宽度,如果已经显示了popup window,修改后不会立即更新。只有下次出现或者调用update()方法才会更新
 void setWindowLayoutMode(int widthSpec, int heightSpec) 
          Change the width and height measure specs that are given to the window manager by the popup.
 //改变popup window管理的window的尺寸
 void showAsDropDown(View anchor) 
          Display the content view in a popup window anchored to the bottom-left corner of the anchor view.
 //anchor意思为锚。
以触发弹出窗的anchor为基准,出现在anchor的正下方,弹出的pop_view左上角正对view的左下角,偏移量默认为0,0
 void showAsDropDown(View anchor, int xoff, int yoff) 
          Display the content view in a popup window anchored to the bottom-left corner of the anchor view offset by the specified x and y coordinates.
 //以anchor的左下角进行偏移,xoff正的向右,负的向左. yoff,正的向下,负的向上
 void showAtLocation(View parent, int gravity, int x, int y) 
           Display the content view in a popup window at the specified location.
  //在特定的地点弹出一个window显示指定的内容
parent:传你当前Layout的id;  
gravity:Gravity.BOTTOM(以屏幕左下角为参照),偏移量会以它为基准点 
当x y为0,0是出现在底部居中
 void update() 
          Updates the state of the popup window, if it is currently being displayed, from the currently set state.
 void update(int x, int y, int width, int height) 
          Updates the position and the dimension of the popup window.
 void update(int x, int y, int width, int height, boolean force) 
          Updates the position and the dimension of the popup window.
 void update(View anchor, int width, int height) 
          Updates the position and the dimension of the popup window.
 void update(View anchor, int xoff, int yoff, int width, int height) 
          Updates the position and the dimension of the popup window.
 //都是更新popup window的尺寸和位置,取决于你是在参数里进行改变,还是调用setWidth等方法改变后再更新
二.PopupWindow设置在指定位置的demo
package com.outdoors.jinghuang.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

/**
 * Created by jing.huang on 2016/12/3.
 */
public class PopupWindowDemo extends Activity{

    private PopupWindow popupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popupwindow_activity);
        findViewById(R.id.show_popupwindow).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPopupWindow();
            }
        });
    }

    /**
     * 显示PopupWindow
     */
    private void showPopupWindow() {
        //设置显示内容
        View contentView = LayoutInflater.from(this).inflate(R.layout.popupwindow_content_view, null);
        popupWindow = new PopupWindow(contentView, 500, 500);
        contentView.findViewById(R.id.normal_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (popupWindow!=null){
                    popupWindow.dismiss();
                    Toast.makeText(PopupWindowDemo.this,getString(R.string.normal_dismiss),Toast.LENGTH_SHORT).show();
                }
            }
        });
        //将PopupWindow显示在底部
        View parentView = LayoutInflater.from(this).inflate(R.layout.popupwindow_activity, null);
        popupWindow.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
    }
}
三.PopupWindow设置在某个控件的下方(以动画的方式从上往下退出)
package com.outdoors.jinghuang.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

/**
 * Created by jing.huang on 2016/12/3.
 */
public class PopupWindowDemo2 extends Activity implements View.OnClickListener {

    private PopupWindow popupWindow;
    private Button showPop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popupwindow2_activity);
        showPop = (Button) findViewById(R.id.show_popupwindow);
        showPop.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.show_popupwindow:
                showPopupWindow();
                break;
            case R.id.top_to_bottom:
                dismissPopupWindow();
                break;
            default:
                break;
        }
    }

    /**
     * 退出PopupWindow
     */
    private void dismissPopupWindow() {
        if (popupWindow != null) {
            popupWindow.dismiss();
            Toast.makeText(this, getString(R.string.top_to_bottom), Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 显示PopupWindow
     */
    private void showPopupWindow() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.popupwindow_demo2, null);
        popupWindow = new PopupWindow(contentView, 500, 500);
        popupWindow.setOutsideTouchable(false);
        contentView.findViewById(R.id.top_to_bottom).setOnClickListener(this);
        popupWindow.setAnimationStyle(R.style.PopupWindowStyle);
        popupWindow.showAsDropDown(showPop);
    }
}

在Res文件夹下创建anim文件夹,然后创建PopupWindow进入和推出的动画
最后在style文件夹下设置PopupWindow的进入和退出的方式。
进入是的动画anim/pop_up_window_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300">
    <translate
        android:fromYDelta="100%p"
        android:toYDelta="0" />
</set>
退出时的动画anim/pop_up_window_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300">
    <translate
        android:fromYDelta="0"
        android:toYDelta="100%p" />
</set>
sytle的代码values/styles.xml
   <style name="PopupWindowStyle">
        <item name="android:windowEnterAnimation">@anim/pop_up_window_in</item>
        <item name="android:windowExitAnimation">@anim/pop_up_window_out</item>
    </style>


 
  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值