Android PopupWindow(一)使用篇

来源:互联网 发布:solrcloud remove node 编辑:程序博客网 时间:2024/05/22 11:31

Android 开发中弹框随处可见,现将自己做过的项目中的PopupWindow,做出一个系统的总结。从以下方面开始总结:

第一篇使用篇:

  1. PopupWindow 的使用.
  2. 示例演示
  3. 与Dialog的区别.

第二篇源码篇:

PopupWindow的原理解析与源码剖析.


有对Dialog的使用不清楚的,请查看我的另外一篇关于Dialog文章:
http://blog.csdn.net/dachaoxuexi/article/details/51274379


PopupWindow的使用

  • 构造方法

    1. public PopupWindow(Context context) {
      this(context, null);
      }
    2. public PopupWindow(Context context, AttributeSet attrs) {
      this(context, attrs, com.android.internal.R.attr.popupWindowStyle);
      }
    3. public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr) {
      this(context, attrs, defStyleAttr, 0);
      }
    4. public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

      }
    5. public PopupWindow() {
      this(null, 0, 0);
      }
    6. public PopupWindow(View contentView) {
      this(contentView, 0, 0);
      }
    7. public PopupWindow(int width, int height) {
      this(null, width, height);
      }
    8. public PopupWindow(View contentView, int width, int height) {
      this(contentView, width, height, false);
      }
      9.public PopupWindow(View contentView, int width, int height, boolean focusable) {}

从构造方法中,可以看出构造最终调用的是public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {},与public PopupWindow(View contentView, int width, int height, boolean focusable) {};分别是自定义与直接设置参数两种情况,无论是哪种情况,想要弹框,都必须具备的条件是:

  1. contentView 要显示的布局
  2. width 布局的宽度
  3. height 布局的高度

注意:因为PopupWindow没有默认布局,所以必须设置contentView .

  • 常用方法

常用方法一显示弹框:

方法一:public void showAtLocation(View parent, int gravity, int x, int y) {    showAtLocation(parent.getWindowToken(), gravity, x, y);}       方法二:public void showAtLocation(IBinder token, int gravity, int x, int y) {...}方法三: public void showAsDropDown(View anchor) {    showAsDropDown(anchor, 0, 0);}方法四:   public void showAsDropDown(View anchor, int xoff, int yoff) {    showAsDropDown(anchor, xoff, yoff, DEFAULT_ANCHORED_GRAVITY);}       方法五:public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {...}

可以看出,显示方法可以归结为两种方式:

第一种,showAtLocation(),相对于父控件parent的位置,可以通过gravity来设置,也可以设置偏移量,通过设置x,y来设置在X,Y轴上的偏移量;亲测parent这个view可以是当前view的随意一个view,不会挂掉.

第二种,showAsDropDown(),相对anchor的位置,从正左下方开始计算,偏移量可以通过int xoff, int yoff设置,gravity是相对于anchor的gravity.

常用方法二设置contentView:

    通过构造方法或者setContentView(View contentView)来设置contentView.

常用方法三设置宽高:

    通过构造方法或者setWidth(int width)和setHeight(int height)来设置.

常用方法四设置focusable:

通过getContentView().setFocusable(true);来设置PopupWindow的焦点.一般设置为true,因为false的话,弹出框不影响当前界面的操作,像不存在一样;注意,假如PopupWindow布局中有editText这种需要焦点的view的话,就必须设置为true.

常用方法五,点击外部关闭PopupWindow:

通过setOutsideTouchable(true)来设置PopupWindow点击外部关闭PopupWindow,但是仅是设置这个方法,会发现弹框并不会关闭,什么原因呢?原因是要设置背景setBackgroundDrawable()才可以,具体原因我会在第二篇原理解析中说明.

常用方法六,添加动画:

    通过setAnimationStyle(int animationStyle)来设置PopupWindow的动画.编写好相应的资源文件就ok了.

PopupWindow实例演示

最开心的就是看效果,OK~~看下面:

示例一,showAsDropDown()演示:

效果图:

down

SimpleExample.java:

import android.app.Activity;import android.graphics.Bitmap;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.PopupWindow;/** *  *@author zyc *created at 2017/02/02 0002 14:27 */public class SimpleExample extends Activity implements View.OnClickListener {    private PopupWindow mPopupWindow;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_simple);        initView();    }    private void initView() {        findViewById(R.id.bt_simple).setOnClickListener(this);        View view = getLayoutInflater().inflate(R.layout.simple_layout, null);        mPopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,true);        //点击空白处的时候PopupWindow会消失        mPopupWindow.setTouchable(true);        mPopupWindow.setOutsideTouchable(true);        mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(),(Bitmap) null));    }    @Override    public void onClick(View view) {        switch (view.getId()){            case R.id.bt_simple:                showPopup(view);                break;        }    }    private void showPopup(View v) {        //以一种向下弹出的动画的形式显示出来        mPopupWindow.showAsDropDown(v);    }}

activity_simple.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:orientation="vertical">    <Button        android:id="@+id/bt_simple"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:layout_marginTop="10dp"        android:text=" 按钮下方弹出" /></LinearLayout>

simple_layout.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:color/black">    <TextView        android:layout_width="wrap_content"        android:layout_height="80dp"        android:layout_centerInParent="true"        android:gravity="center"        android:text="dachao 你最棒!"        android:textColor="#ffffffff" /></RelativeLayout>

示例二,showAtLocation()以及进入退出动画:

效果图:

showAtLocation

BottomAnim.java

import android.app.Activity;import android.graphics.Bitmap;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.view.Gravity;import android.view.KeyEvent;import android.view.View;import android.view.ViewGroup;import android.widget.PopupWindow;/** * *@author zyc *created at 2017/02/02 0002 16:11 */public class BottomAnim extends Activity implements View.OnClickListener {    private PopupWindow mPopupWindow;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_bottom);        initView();    }    private void initView() {        findViewById(R.id.bt_bottom).setOnClickListener(this);        View popupView = getLayoutInflater().inflate(R.layout.simple_layout, null);        mPopupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);        mPopupWindow.setTouchable(true);        mPopupWindow.setOutsideTouchable(true);        mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));        mPopupWindow.setAnimationStyle(R.style.anim_menu_bottombar);        mPopupWindow.getContentView().setFocusableInTouchMode(true);        mPopupWindow.getContentView().setFocusable(true);        mPopupWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {            @Override            public boolean onKey(View v, int keyCode, KeyEvent event) {                if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0                        && event.getAction() == KeyEvent.ACTION_DOWN) {                    if (mPopupWindow != null && mPopupWindow.isShowing()) {                        mPopupWindow.dismiss();                    }                    return true;                }                return false;            }        });    }    @Override    public void onClick(View view) {        mPopupWindow.showAtLocation(findViewById(R.id.bt_bottom), Gravity.BOTTOM, 0, 0);    }

activity_bottom.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/bt_bottom"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:layout_gravity="center_horizontal"        android:text="底部弹出"/></LinearLayout>

simple_layout.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:color/black">    <TextView        android:layout_width="wrap_content"        android:layout_height="80dp"        android:layout_centerInParent="true"        android:gravity="center"        android:text="dachao 你最棒!"        android:textColor="#ffffffff" /></RelativeLayout>

anim_menu_bottombar:

  // 设置动画    <style name="anim_menu_bottombar">        <item name="android:windowEnterAnimation">@anim/menu_bottom_in</item>        <item name="android:windowExitAnimation">@anim/menu_bottom_out</item>    </style>

menu_bottom_in.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    android:duration="300"    android:fromYDelta="100.0%"    android:toYDelta="0.0" /></set>

menu_bottom_out.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:duration="300"        android:fromYDelta="0.0"        android:toYDelta="100%" /></set>
1 0
原创粉丝点击