Android 中Popwindow彈出菜單的兩種方法實(shí)例
Android 中Popwindow彈出菜單的兩種方法實(shí)例
1.popWindow就是對(duì)話框的一種方式!
此文講解的android中對(duì)話框的一種使用方式,它叫popWindow。
2、popWindow的特性
Android的對(duì)話框有兩種:PopupWindow和AlertDialog。它們的不同點(diǎn)在于:
AlertDialog的位置固定,而PopupWindow的位置可以隨意。
AlertDialog是非阻塞線程的,而PopupWindow是阻塞線程的。
PopupWindow的位置按照有無(wú)偏移分,可以分為偏移和無(wú)偏移兩種;按照參照物的不同,可以分為相對(duì)于某個(gè)控件(Anchor錨)和相對(duì)于父控件。
實(shí)例代碼:
方法一的Activity
package com.app.test02; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.PopupWindow; import android.widget.Toast; public class PopwindowLeft extends Activity { // 聲明PopupWindow對(duì)象的引用 private PopupWindow popupWindow; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_popupwindow_main); // 點(diǎn)擊按鈕彈出菜單 Button pop = (Button) findViewById(R.id.popBtn); pop.setOnClickListener(popClick); } // 點(diǎn)擊彈出左側(cè)菜單的顯示方式 OnClickListener popClick = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub getPopupWindow(); // 這里是位置顯示方式,在屏幕的左側(cè) popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0); } }; /** * 創(chuàng)建PopupWindow */ protected void initPopuptWindow() { // TODO Auto-generated method stub // 獲取自定義布局文件activity_popupwindow_left.xml的視圖 View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null, false); // 創(chuàng)建PopupWindow實(shí)例,200,LayoutParams.MATCH_PARENT分別是寬度和高度 popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true); // 設(shè)置動(dòng)畫(huà)效果 popupWindow.setAnimationStyle(R.style.AnimationFade); // 點(diǎn)擊其他地方消失 popupWindow_view.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); popupWindow = null; } return false; } }); } /*** * 獲取PopupWindow實(shí)例 */ private void getPopupWindow() { if (null != popupWindow) { popupWindow.dismiss(); return; } else { initPopuptWindow(); } } }
方法二的Activity
package com.app.test02; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup.LayoutParams; import android.widget.PopupWindow; public class PopwindowLeftNew extends Activity{ private PopupWindow popupWindow; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_popupwindow_main); findViewById(R.id.popBtn).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 獲取自定義布局文件activity_popupwindow_left.xml的視圖 View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,false); // 創(chuàng)建PopupWindow實(shí)例,200,LayoutParams.MATCH_PARENT分別是寬度和高度 popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true); // 設(shè)置動(dòng)畫(huà)效果 popupWindow.setAnimationStyle(R.style.AnimationFade); // 這里是位置顯示方式,在屏幕的左側(cè) popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0); // 點(diǎn)擊其他地方消失 popupWindow_view.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); popupWindow = null; } return false; } }); } }); } }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Android SDK Manager更新、下載速度慢問(wèn)題解決辦法
這篇文章主要介紹了Android SDK Manager更新、下載速度慢問(wèn)題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-05-05Android 處理OnItemClickListener時(shí)關(guān)于焦點(diǎn)顏色的設(shè)置問(wèn)題
這篇文章主要介紹了Android 處理OnItemClickListener時(shí)關(guān)于焦點(diǎn)顏色的設(shè)置問(wèn)題的相關(guān)資料,需要的朋友可以參考下2017-02-02Android?音頻波形圖實(shí)現(xiàn)效果示例
這篇文章主要為大家介紹了Android?音頻波形圖實(shí)現(xiàn)效果示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08android配合viewpager實(shí)現(xiàn)可滑動(dòng)的標(biāo)簽欄示例分享
本文主要介紹了android實(shí)現(xiàn)可滑動(dòng)的標(biāo)簽欄示例,配合viewpager作為標(biāo)簽欄,且可以設(shè)置每頁(yè)顯示的標(biāo)簽個(gè)數(shù),超出可滑動(dòng)顯示,需要的朋友可以參考下2014-02-02Android開(kāi)發(fā)之MediaPlayer基本使用方法詳解
這篇文章主要介紹了Android開(kāi)發(fā)之MediaPlayer基本使用方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了MediaPlayer中的常用函數(shù)與基本使用技巧,需要的朋友可以參考下2017-05-05Android高仿微信對(duì)話列表滑動(dòng)刪除效果
這篇文章主要為大家詳細(xì)介紹了Android高仿微信對(duì)話列表滑動(dòng)刪除效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08Android判斷后臺(tái)服務(wù)是否開(kāi)啟的兩種方法實(shí)例詳解
這篇文章主要介紹了Android判斷后臺(tái)服務(wù)是否開(kāi)啟的方法的相關(guān)資料,這里提供了兩種方法及實(shí)例,需要的朋友可以參考下2017-07-07