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

PopupWindow自定義位置顯示的實現(xiàn)代碼

 更新時間:2017年10月30日 14:34:44   作者:與我常在i  
這篇文章主要為大家詳細(xì)介紹了PopupWindow自定義位置顯示,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、概述

在Android中彈出式菜單(以下稱彈窗)是使用十分廣泛的一種菜單呈現(xiàn)方式,彈窗為用戶交互提供了便利。關(guān)于彈窗的實現(xiàn)大致有以下兩種方式AlertDialog和PopupWindow,當(dāng)然網(wǎng)上也有使用Activity并配合Dialog主題的方式實現(xiàn)彈窗,有興趣的朋友也可以去研究一下。對于AlertDialog和PopupWindow兩者最主要的區(qū)別就是顯示的位置問題:

(1)AlertDialog在位置顯示上是固定的
(2)PopupWindow相對比較隨意,能夠在主屏幕的任意位置顯示。

二、效果圖

這里寫圖片描述

三、代碼

(1)MainActivity中的代碼:

public class MainActivity extends AppCompatActivity {

  private int x;
  private int y;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {

    // 獲得點擊屏幕的坐標(biāo)

    x = (int) event.getX();
    y = (int) event.getY();

    // 加載PopupWindow 對應(yīng)的界面
    LayoutInflater inflater = getLayoutInflater();
    final View popupView = inflater.inflate(R.layout.popup_entry_layout,null);

    // 創(chuàng)建PopupWindow 對象
    final PopupWindow popupWindow = new PopupWindow(popupView,400,100); // 第二、第三個參數(shù)用來設(shè)置彈窗的大小,也可以用WRAP_CONTENT

    // 設(shè)置位置
    popupWindow.showAtLocation(popupView, Gravity.NO_GRAVITY,x,y);

    new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {

        // 1秒后關(guān)閉該彈窗

        popupWindow.dismiss();

      }
    },1000);

    return true;
  }
}

(2)布局文件中的代碼省略。

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

相關(guān)文章

最新評論