欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android popupwindow用法詳解

 更新時間:2019年10月28日 08:59:11   作者:我的心里只有你  
這篇文章主要為大家詳細介紹了android popupwindow用法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android Studio手動打包的教程圖解

    Android Studio手動打包的教程圖解

    項目寫完了,現(xiàn)在需要把應(yīng)用上傳到市場上面,那么怎么把項目打包成apk呢?下面腳本之家小編給大家?guī)砹薃ndroid Studio手動打包的方法,一起看看吧
    2018-07-07
  • Flutter組件生命周期和App生命周期示例解析

    Flutter組件生命周期和App生命周期示例解析

    這篇文章主要為大家介紹了Flutter組件生命周期和App生命周期示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Android接入阿里云熱修復(fù)介紹

    Android接入阿里云熱修復(fù)介紹

    大家好,本篇文章主要講的是Android接入阿里云熱修復(fù)介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Android 實現(xiàn)手機撥打電話的功能

    Android 實現(xiàn)手機撥打電話的功能

    本篇文章主要介紹 Android 開發(fā)手機撥打電話的功能,這里提供示例代碼,有興趣的小伙伴可以參考下
    2016-08-08
  • Android?控件自動貼邊實現(xiàn)實例詳解

    Android?控件自動貼邊實現(xiàn)實例詳解

    這篇文章主要為大家介紹了Android?控件自動貼邊實現(xiàn)實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Android開發(fā)工程中集成mob短信驗證碼功能的方法

    Android開發(fā)工程中集成mob短信驗證碼功能的方法

    這篇文章主要介紹了Android開發(fā)工程中集成mob短信驗證碼功能的方法,感興趣的小伙伴們可以參考一下
    2016-05-05
  • flutter實現(xiàn)一個列表下拉抽屜的示例代碼

    flutter實現(xiàn)一個列表下拉抽屜的示例代碼

    本文主要介紹了flutter實現(xiàn)一個列表下拉抽屜的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Android編程實現(xiàn)播放音頻的方法示例

    Android編程實現(xiàn)播放音頻的方法示例

    這篇文章主要介紹了Android編程實現(xiàn)播放音頻的方法,結(jié)合實例形式分析了Android使用MediaPlayer類播放音頻的相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • Android自定義控件實現(xiàn)時間軸

    Android自定義控件實現(xiàn)時間軸

    這篇文章主要為大家詳細介紹了Android自定義控件實現(xiàn)時間軸,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Android自動播放Banner圖片輪播效果

    Android自動播放Banner圖片輪播效果

    這篇文章主要介紹了Android自動播放Banner圖片輪播效果的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-07-07

最新評論