一聚教程网:一个值得你收藏的教程网站

热门教程

Android 中Popwindow弹出菜单的两种方法实例

时间:2022-06-25 23:24:21 编辑:袖梨 来源:一聚教程网

Android 中Popwindow弹出菜单的两种方法实例

1.popWindow就是对话框的一种方式!

此文讲解的android中对话框的一种使用方式,它叫popWindow。

2、popWindow的特性

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

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

AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的。

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

实例代码:

方法一的Activity

 

 代码如下复制代码

packagecom.app.test02;

  

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.Gravity;

importandroid.view.MotionEvent;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.view.View.OnTouchListener;

importandroid.view.ViewGroup.LayoutParams;

importandroid.widget.Button;

importandroid.widget.PopupWindow;

importandroid.widget.Toast;

  

publicclassPopwindowLeftextendsActivity {

  // 声明PopupWindow对象的引用

  privatePopupWindow popupWindow;

  

  /** Called when the activity is first created. */

  @Override

  publicvoidonCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_popupwindow_main);

    // 点击按钮弹出菜单

    Button pop = (Button) findViewById(R.id.popBtn);

    pop.setOnClickListener(popClick);

  }

  

  // 点击弹出左侧菜单的显示方式

  OnClickListener popClick =newOnClickListener() {

    @Override

    publicvoidonClick(View v) {

      // TODO Auto-generated method stub

      getPopupWindow();

      // 这里是位置显示方式,在屏幕的左侧

      popupWindow.showAtLocation(v, Gravity.LEFT,0,0);

    }

  };

  

  /**

   * 创建PopupWindow

   */

  protectedvoidinitPopuptWindow() {

    // TODO Auto-generated method stub

    // 获取自定义布局文件activity_popupwindow_left.xml的视图

    View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left,null,

        false);

    // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度

    popupWindow =newPopupWindow(popupWindow_view,200, LayoutParams.MATCH_PARENT,true);

    // 设置动画效果

    popupWindow.setAnimationStyle(R.style.AnimationFade);

    // 点击其他地方消失

    popupWindow_view.setOnTouchListener(newOnTouchListener() {

      @Override

      publicbooleanonTouch(View v, MotionEvent event) {

        // TODO Auto-generated method stub

        if(popupWindow !=null&& popupWindow.isShowing()) {

          popupWindow.dismiss();

          popupWindow =null;

        }

        returnfalse;

      }

    });

  }

  /***

   * 获取PopupWindow实例

   */

  privatevoidgetPopupWindow() {

    if(null!= popupWindow) {

      popupWindow.dismiss();

      return;

    }else{

      initPopuptWindow();

    }

  }

}

 

方法二的Activity

 

 代码如下复制代码

packagecom.app.test02;

  

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.Gravity;

importandroid.view.MotionEvent;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.view.View.OnTouchListener;

importandroid.view.ViewGroup.LayoutParams;

importandroid.widget.PopupWindow;

  

publicclassPopwindowLeftNewextendsActivity{

  privatePopupWindow popupWindow;

  @Override

  protectedvoidonCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_popupwindow_main);

      

    findViewById(R.id.popBtn).setOnClickListener(newOnClickListener() {

      @Override

      publicvoidonClick(View v) {

        // TODO Auto-generated method stub

        // 获取自定义布局文件activity_popupwindow_left.xml的视图

        View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left,null,false);

        // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度

        popupWindow =newPopupWindow(popupWindow_view,200, LayoutParams.MATCH_PARENT,true);

        // 设置动画效果

        popupWindow.setAnimationStyle(R.style.AnimationFade);

        // 这里是位置显示方式,在屏幕的左侧

        popupWindow.showAtLocation(v, Gravity.LEFT,0,0);

        // 点击其他地方消失

        popupWindow_view.setOnTouchListener(newOnTouchListener() {

          @Override

          publicbooleanonTouch(View v, MotionEvent event) {

            // TODO Auto-generated method stub

            if(popupWindow !=null&& popupWindow.isShowing()) {

              popupWindow.dismiss();

              popupWindow =null;

            }

            returnfalse;

          }

        });

      }

    });

      

  }

}

 

热门栏目