当前位置: 移动技术网 > IT编程>移动开发>Android > Android自定义弹出窗口PopupWindow使用技巧

Android自定义弹出窗口PopupWindow使用技巧

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

without you 歌词,夫妻轩,大数据旅行新变化

popupwindow是android上自定义弹出窗口,使用起来很方便。

popupwindow的构造函数为

复制代码 代码如下:
public popupwindow(view contentview, int width, int height, boolean focusable)

contentview为要显示的view,width和height为宽和高,值为像素值,也可以是matcht_parent和wrap_content。

focusable为是否可以获得焦点,这是一个很重要的参数,也可以通过public void setfocusable(boolean focusable)来设置,如果focusable为false,在一个activity弹出一个popupwindow,按返回键,由于popupwindow没有焦点,会直接退出activity。如果focusable为true,popupwindow弹出后,所有的触屏和物理按键都有popupwindows处理。

如果popupwindow中有editor的话,focusable要为true。

下面实现一个简单的popupwindow

主界面的layout为:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/layout_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingbottom="@dimen/activity_vertical_margin"
 android:paddingleft="@dimen/activity_horizontal_margin"
 android:paddingright="@dimen/activity_horizontal_margin"
 android:paddingtop="@dimen/activity_vertical_margin"
 tools:context=".mainactivity" >

 <button
  android:id="@+id/btn_test_popupwindow"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerinparent="true"
  android:text="@string/app_name" />

</relativelayout>

popupwindow的layout为:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="#000000" >

 <textview
  android:layout_width="wrap_content"
  android:layout_height="80dp"
  android:text="@string/app_name" 
  android:textcolor="#ffffffff"
  android:layout_centerinparent="true"
  android:gravity="center"/>

</relativelayout>

activity的代码为:

public class mainactivity extends activity {

 private button mbutton;
 private popupwindow mpopupwindow;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  view popupview = getlayoutinflater().inflate(r.layout.layout_popupwindow, null);

  mpopupwindow = new popupwindow(popupview, layoutparams.match_parent, layoutparams.wrap_content, true);
  mpopupwindow.settouchable(true);
  mpopupwindow.setoutsidetouchable(true);
  mpopupwindow.setbackgrounddrawable(new bitmapdrawable(getresources(), (bitmap) null));

  mbutton = (button) findviewbyid(r.id.btn_test_popupwindow);
  mbutton.setonclicklistener(new onclicklistener() {

   @override
   public void onclick(view v) {
    mpopupwindow.showasdropdown(v);
   }
  });
 }
}

这三行代码

mpopupwindow.settouchable(true);
mpopupwindow.setoutsidetouchable(true);
mpopupwindow.setbackgrounddrawable(new bitmapdrawable(getresources(), (bitmap) null));


的作用是点击空白处的时候popupwindow会消失。

mpopupwindow.showasdropdown(v);
这一行代码将popupwindow以一种向下弹出的动画的形式显示出来

public void showasdropdown(view anchor, int xoff, int yoff)
这个函数的第一个参数为一个view,我们这里是一个button,那么popupwindow会在这个button下面显示,xoff,yoff为显示位置的偏移。

点击按钮,就会显示出popupwindow

很多时候我们把popupwindow用作自定义的菜单,需要一个从底部向上弹出的效果,这就需要为popupwindow添加动画。

在工程res下新建anim文件夹,在anim文件夹先新建两个xml文件

menu_bottombar_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

 <translate
  android:duration="250"
  android:fromydelta="100.0%"
  android:toydelta="0.0" />

</set>

menu_bottombar_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

 <translate
  android:duration="250"
  android:fromydelta="0.0"
  android:toydelta="100%" />

</set>

在res/value/styles.xml添加一个sytle

 <style name="anim_menu_bottombar">
  <item name="android:windowenteranimation">@anim/menu_bottombar_in</item>
  <item name="android:windowexitanimation">@anim/menu_bottombar_out</item>
 </style>

acivity修改为

public class mainactivity extends activity {

 private popupwindow mpopupwindow;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  view popupview = getlayoutinflater().inflate(r.layout.layout_popupwindow, null);

  mpopupwindow = new popupwindow(popupview, layoutparams.match_parent, layoutparams.wrap_content, true);
  mpopupwindow.settouchable(true);
  mpopupwindow.setoutsidetouchable(true);
  mpopupwindow.setbackgrounddrawable(new bitmapdrawable(getresources(), (bitmap) null));

  mpopupwindow.getcontentview().setfocusableintouchmode(true);
  mpopupwindow.getcontentview().setfocusable(true);
  mpopupwindow.getcontentview().setonkeylistener(new 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 boolean onkeydown(int keycode, keyevent event) {
  if (keycode == keyevent.keycode_menu && event.getrepeatcount() == 0) {
   if (mpopupwindow != null && !mpopupwindow.isshowing()) {
    mpopupwindow.showatlocation(findviewbyid(r.id.layout_main), gravity.bottom, 0, 0);
   }
   return true;
  }
  return super.onkeydown(keycode, event);
 }
}

这样点击菜单键会弹出自定义的popupwindow,点击空白处或者返回键、菜单键,popupwindow会消失。

文章如果有不对的地方,希望大家理解。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

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

相关文章:

验证码:
移动技术网