欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

Android

當(dāng)前位置:主頁 > 軟件編程 > Android >

android popupwindow用法詳解

來源:本站原創(chuàng)|時間:2020-01-10|欄目:Android|點擊: 次

本文實例為大家分享了android popupwindow的用法,供大家參考,具體內(nèi)容如下

一、基本用法

一般做法,新建類繼承popupwindow。例

/**
 * popupwindow基本用法
 * Created by Administrator on 2015/11/25.
 */
public class DemoBasePop extends PopupWindow {
  private LinearLayout linear_layout;
  private TextView dbp_text;
  private Context context;
  public DemoBasePop(final Activity context) {
    super(context);
    this.context = context;
    View view = LayoutInflater.from(context).inflate(R.layout.demo_base_pop,null);
 
    setContentView(view);
    setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    setHeight(200);
//    setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
 
    setFocusable(true);
    setBackgroundDrawable(new BitmapDrawable());
    setTouchable(true);
    setOutsideTouchable(true);
    setAnimationStyle(R.style.popwin_anim_style);
//    setAnimationStyle(0);   0是沒有animation
 
    initView(view);
 
  }
 
  private void initView(View view) {
    dbp_text = (TextView) view.findViewById(R.id.dbp_text);
  }
 
}

研究下popupwindow源碼,以showAsDropDown來講

public void showAsDropDown(View anchor, int xoff, int yoff) {
    if (isShowing() || mContentView == null) {
      return;
    }
 
    registerForScrollChanged(anchor, xoff, yoff);
 
    mIsShowing = true;
    mIsDropdown = true;
 
    WindowManager.LayoutParams p = createPopupLayout(anchor.getWindowToken());
    preparePopup(p);
 
    updateAboveAnchor(findDropDownPosition(anchor, p, xoff, yoff));
 
    if (mHeightMode < 0) p.height = mLastHeight = mHeightMode;
    if (mWidthMode < 0) p.width = mLastWidth = mWidthMode;
 
    p.windowAnimations = computeAnimationResource();
 
    invokePopup(p);
  }

第11行創(chuàng)建WindowManager.LayoutParams。第12行preparePopup()中:

if (mBackground != null) {
      final ViewGroup.LayoutParams layoutParams = mContentView.getLayoutParams();
      int height = ViewGroup.LayoutParams.MATCH_PARENT;
      if (layoutParams != null &&
          layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
        height = ViewGroup.LayoutParams.WRAP_CONTENT;
      }
 
      // when a background is available, we embed the content view
      // within another view that owns the background drawable
      PopupViewContainer popupViewContainer = new PopupViewContainer(mContext);
      PopupViewContainer.LayoutParams listParams = new PopupViewContainer.LayoutParams(
          ViewGroup.LayoutParams.MATCH_PARENT, height
      );
      popupViewContainer.setBackgroundDrawable(mBackground);
      popupViewContainer.addView(mContentView, listParams);
 
      mPopupView = popupViewContainer;
    } else {
      mPopupView = mContentView;
    }

如果做了setBackgroundDrawable(new BitmapDrawable());那么mBackground則不為空,則會用PopupViewContainer作為mPopupView(即內(nèi)容view)。而PopupViewContainer的dispatchKeyEvent對返回鍵做了處理,按返回鍵后其中調(diào)用dismiss()方法。其onTouchEvent對觸摸事件做了處理,其源碼:

public boolean onTouchEvent(MotionEvent event) {
      final int x = (int) event.getX();
      final int y = (int) event.getY();
      <span style="font-family: 宋體; font-size: 9pt;">//點擊外部隱藏</span>
      if ((event.getAction() == MotionEvent.ACTION_DOWN)
          && ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) {
        dismiss();
        return true;
      } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
        dismiss();
        return true;
      } else {
        return super.onTouchEvent(event);
      }
    }

系統(tǒng)做了這些處理,隨之而來一個問題,如果我們要監(jiān)聽物理返回鍵該怎么辦??戳松厦娴倪^程,我們可以想到將

setBackgroundDrawable(null);然后通過設(shè)置view的key監(jiān)聽,監(jiān)聽到后做相應(yīng)的處理。
view.setOnKeyListener(new View.OnKeyListener() {
      @Override
      public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
          if (event.getAction() == KeyEvent.ACTION_DOWN
              && event.getRepeatCount() == 0) {
            outAnimator.start();
            return true;
          }
        }
        return false;
      }
    });

效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:Android開發(fā)之基于RecycleView實現(xiàn)的頭部懸浮控件

欄    目:Android

下一篇:Android自定義酒店日期選擇器

本文標(biāo)題:android popupwindow用法詳解

本文地址:http://mengdiqiu.com.cn/a1/Android/9138.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有