最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)聽到后做相應的處理。
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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android?使用壓縮紋理的方案

    Android?使用壓縮紋理的方案

    這篇文章主要介紹了Android?使用壓縮紋理,本文介紹了什么是壓縮紋理,以及加載壓縮紋理的核心步驟,并在 Android OpenGLES 平臺上實現(xiàn)了壓縮紋理的顯示,需要的朋友可以參考下
    2022-09-09
  • activitygroup 切換動畫效果如何實現(xiàn)

    activitygroup 切換動畫效果如何實現(xiàn)

    本文將詳細介紹activitygroup 切換動畫效果實現(xiàn)過程,需要聊解的朋友可以參考下
    2012-12-12
  • Android中底部菜單被輸入法頂上去的解決方案

    Android中底部菜單被輸入法頂上去的解決方案

    我們一般的解決方法是獲取焦點,底部隱藏,失去焦點,底部菜單出現(xiàn),但是,有些人會點擊這個按鈕收起鍵牌。這篇文章主要介紹了Android中底部菜單被輸入法頂上去的解決方案,需要的朋友參考下吧
    2017-01-01
  • android實現(xiàn)漢字轉(zhuǎn)拼音功能 帶多音字識別

    android實現(xiàn)漢字轉(zhuǎn)拼音功能 帶多音字識別

    這篇文章主要介紹了android實現(xiàn)漢字轉(zhuǎn)拼音功能,帶多音字識別,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android組件實現(xiàn)列表選擇框功能

    Android組件實現(xiàn)列表選擇框功能

    android提供的列表選擇框(Spinner)相當于web端用戶注冊時的選擇下拉框,比如注冊候選擇省份城市等。這篇文章主要介紹了Android組件實現(xiàn)列表選擇框功能,需要的朋友可以參考下
    2017-02-02
  • Android服務應用ClockService實現(xiàn)鬧鐘功能

    Android服務應用ClockService實現(xiàn)鬧鐘功能

    這篇文章主要為大家詳細介紹了Android服務應用ClockService實現(xiàn)鬧鐘功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • android檢查手機和無線是否連接的方法

    android檢查手機和無線是否連接的方法

    這篇文章主要介紹了android檢查手機和無線是否連接的方法,以兩種不同的方法實現(xiàn)了該功能,是Android程序開發(fā)中非常常見的實用技巧,需要的朋友可以參考下
    2014-10-10
  • Android移動端touch實現(xiàn)下拉刷新功能

    Android移動端touch實現(xiàn)下拉刷新功能

    這篇文章主要介紹了移動端touch實現(xiàn)下拉刷新功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-02-02
  • Android自定義控件實現(xiàn)支付寶記賬餅圖

    Android自定義控件實現(xiàn)支付寶記賬餅圖

    這篇文章主要為大家詳細介紹了Android自定義控件實現(xiàn)支付寶記賬餅圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • 完美解決安卓jni項目會刪除其他so文件的問題

    完美解決安卓jni項目會刪除其他so文件的問題

    下面小編就為大家?guī)硪黄昝澜鉀Q安卓jni項目會刪除其他so文件的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12

最新評論

鲜城| 东乌珠穆沁旗| 临江市| 陇西县| 罗山县| 镇沅| 四子王旗| 饶河县| 衡东县| 泸水县| 甘孜县| 嫩江县| 西丰县| 睢宁县| 黔西县| 浪卡子县| 宣威市| 汉中市| 通河县| 宜黄县| 新野县| 阿拉善左旗| 玉田县| 镇巴县| 安福县| 航空| 广安市| 瑞安市| 周至县| 平舆县| 金乡县| 客服| 西藏| 海阳市| 宜君县| 崇礼县| 岳阳市| 九台市| 连江县| 绥德县| 马边|