PopupWindow自定義位置顯示的實現(xiàn)代碼
一、概述
在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)文章
Android基于注解的6.0權(quán)限動態(tài)請求框架詳解
這篇文章主要介紹了Android基于注解的6.0權(quán)限動態(tài)請求框架詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04Android實現(xiàn)Banner界面廣告圖片循環(huán)輪播(包括實現(xiàn)手動滑動循環(huán))
這篇文章主要介紹了Android實現(xiàn)Banner界面廣告圖片循環(huán)輪播(包括實現(xiàn)手動滑動循環(huán))的相關(guān)資料,需要的朋友可以參考下2016-02-02Android Flutter實現(xiàn)上拉加載組件的示例代碼
既然列表有下拉刷新外當(dāng)然還有上拉加載更多操作了,本次就為大家詳細(xì)介紹如何利用Flutter實現(xiàn)為列表增加上拉加載更多的交互,感興趣的可以了解一下2022-08-08