Popupwindow怎么在Android应用中使用

这期内容当中小编将会给大家带来有关Popupwindow怎么在Android应用中使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

站在用户的角度思考问题,与客户深入沟通,找到扬州网站设计与扬州网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计、成都做网站、企业官网、英文网站、手机端网站、网站推广、域名申请、虚拟空间、企业邮箱。业务覆盖扬州地区。

第一步:

private PopupWindow mPopupWindow;

第二步:写一个popupwindow的布局文件XML

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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怎么在Android应用中使用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。

网站标题:Popupwindow怎么在Android应用中使用
当前地址:https://www.cdcxhl.com/article16/gphhgg.html

成都网站建设公司_创新互联,为您提供微信公众号搜索引擎优化网站建设App设计虚拟主机外贸建站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站制作