当前位置: 移动技术网 > 移动技术>移动开发>Android > Popupwindow 的简单实用案例(显示在控件下方)

Popupwindow 的简单实用案例(显示在控件下方)

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

第一步:

private popupwindow mpopupwindow;

第二步:写一个popupwindow的布局文件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">
  <relativelayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#669e9e9e">
<linearlayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:background="#e4e4e4"
  >
  <textview
    android:id="@+id/popupwindow_jan"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="一月份"
    android:gravity="center"
    />
  <textview
    android:id="@+id/popupwindow_feb"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="二月份"
    android:gravity="center"
    />
  <textview
    android:id="@+id/popupwindow_mar"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="三月份"
    android:gravity="center"
    />
</linearlayout>
  </relativelayout>
</linearlayout>

第三步:在activity写代码

public void onclick(view v) {
  switch (v.getid()) {
   case r.id.home_travel_modes_yuefen_textview:
       showpopupwindow(v);
      break;
   case r.id.popupwindow_jan:
      showtoastmsg("一月份");
      break;
    case r.id.popupwindow_feb:
      showtoastmsg("二月份");
      break;
    default:
      break;
  }

 public void showpopupwindow(view v){
    view contentview = layoutinflater.from(hometravelmodesactivity.this).inflate(r.layout.home_popuplayout, null);
    textview jantext = (textview)contentview.findviewbyid(r.id.popupwindow_jan);
    textview febtext = (textview)contentview.findviewbyid(r.id.popupwindow_feb);
    textview martext = (textview)contentview.findviewbyid(r.id.popupwindow_mar);
    jantext.setonclicklistener(this);
    febtext.setonclicklistener(this);
    martext.setonclicklistener(this);
    final popupwindow popupwindow = new popupwindow(contentview,
        linearlayout.layoutparams.match_parent, 300, true);
    popupwindow.settouchable(true);

//    popupwindow.settouchinterceptor(new view.ontouchlistener() {
//
//      @override
//      public boolean ontouch(view v, motionevent event) {
//
//        log.i("mengdd", "ontouch : ");
//
//        return false;
//        // 这里如果返回true的话,touch事件将被拦截
//        // 拦截后 popupwindow的ontouchevent不被调用,这样点击外部区域无法dismiss
//      }
//    });
    // 如果不设置popupwindow的背景,无论是点击外部区域还是back键都无法dismiss弹框
    // 我觉得这里是api的一个bug
    popupwindow.setbackgrounddrawable(getresources().getdrawable(
        r.mipmap.ic_launcher));
    // 设置好参数之后再show
    popupwindow.showasdropdown(v);
  }

注:

若在activity的oncreate()方法中直接写弹出popupwindow()方法报错,因为activity没有完全启动是不能弹出popupwindow的,那我们只需要在activity完全启动后在弹出popupwindow就行了。

重写一下onwindowfocuschanged()方法:

@override
public void onwindowfocuschanged(boolean hasfocus) {
  super.onwindowfocuschanged(hasfocus);
  //弹出popupwindow的具体代码
}

以上这篇popupwindow 的简单实用案例(显示在控件下方)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网