当前位置: 移动技术网 > IT编程>移动开发>Android > Android开发之PopupWindow创建弹窗、对话框的方法详解

Android开发之PopupWindow创建弹窗、对话框的方法详解

2019年07月23日  | 移动技术网IT编程  | 我要评论

我的叔叔于勒教学设计,九道湾大峡谷,072713-392

本文实例讲述了android开发之popupwindow创建弹窗、对话框的方法。分享给大家供大家参考,具体如下:

简介:

popupwindow 可创建类似对话框风格的窗口

效果:

使用方法:

使用popupwindow 创建对话框风格的串口秩序如下两步即可:

1. popupwindow 的构造器创建popupwindow对象

2. popupwindow 的showasdropdown() 将其显示效果设置为下拉显示

3. popupwindow 的showatloacation() 方法将popupwindow() 在指定位置显示出来

下拉显示效果:

具体实现方法:

public class mainactivity extends activity {
  private popupwindow popupwindow;
  private view root;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    root = this.getlayoutinflater().inflate(r.layout.cell,null);//add cell.xml above you mainactivity window
    popupwindow = new popupwindow(root,560,700);//create a popupwindow object
    root.findviewbyid(r.id.button01).setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        //close the popupwindow
        popupwindow.dismiss();
      }
    });
  }
  public void send(view source){
    //set the location of popupwindow
    popupwindow.showatlocation(findviewbyid(r.id.send),gravity.center,20,20);//you can remove this effect
    //use dropdown way to display
    popupwindow.showasdropdown(root);
  }
}

mainactivity的布局文件:

<?xml version="1.0" encoding="utf-8" ?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/idtatabhost"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="1">
  <button
    android:id="@+id/send"
    android:onclick="send"
    android:text="点我一下 有惊喜(吓) 。。。"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</linearlayout>

/layout/cell.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
  android:id="@+id/cell"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical">
  <imageview
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="9"
    android:src="@drawable/wechat"
    android:scaletype="fitxy"/>
  <button
    android:id="@+id/button01"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#ffffffff"
    android:text="close"
    android:textsize="15dp"/>
</linearlayout>

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网