亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

一、概述

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

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

二、效果圖

這里寫圖片描述

三、代碼

(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) {

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

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

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

    // 創(chuàng)建PopupWindow 對(duì)象
    final PopupWindow popupWindow = new PopupWindow(popupView,400,100); // 第二、第三個(gè)參數(shù)用來(lái)設(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)布局文件中的代碼省略。

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

相關(guān)文章

最新評(píng)論