Android自定義PopupWindow小案例
PopupWindow是我們開發(fā)中的??椭唬褂闷饋硪脖容^簡單方便。
寫了個最簡單地自定義PopupWindow,記錄下來,方便以后使用時直接在上面改動就可以。
/** * @param * @author ldm * @description 自定義PopupWindow * @time 2016/9/29 15:26 */ public class CustomPopup extends PopupWindow { //上下文 private Context mContext; // PopupWindow中控件點擊事件回調(diào)接口 private IPopuWindowListener mOnClickListener; //PopupWindow布局文件中的Button private Button alarm_pop_btn; /** * @description 構(gòu)造方法 * @author ldm * @time 2016/9/30 9:14 * @param */ public CustomPopup(Context mContext, int width, int height, IPopuWindowListener listener) { super(mContext); this.mContext = mContext; this.mOnClickListener = listener; //獲取布局文件 View mContentView = LayoutInflater.from(mContext).inflate(R.layout.alarm_disopse_pop, null); //設(shè)置布局 setContentView(mContentView); // 設(shè)置彈窗的寬度和高度 setWidth(width); setHeight(height); //設(shè)置能否獲取到焦點 setFocusable(false); //設(shè)置PopupWindow進(jìn)入和退出時的動畫效果 setAnimationStyle(R.style.popwindow_exit_anim_style); setTouchable(true); // 默認(rèn)是true,設(shè)置為false,所有touch事件無響應(yīng),而被PopupWindow覆蓋的Activity部分會響應(yīng)點擊 // 設(shè)置彈窗外可點擊,此時點擊PopupWindow外的范圍,Popupwindow不會消失 setOutsideTouchable(false); //外部是否可以點擊,設(shè)置Drawable原因可以參考:http://blog.csdn.net/harvic880925/article/details/49278705 setBackgroundDrawable(new BitmapDrawable()); // 設(shè)置彈窗的布局界面 initUI(); } /** * 初始化彈窗列表 */ private void initUI() { //獲取到按鈕 alarm_pop_btn = (Button) getContentView().findViewById(R.id.alarm_pop_btn); //設(shè)置按鈕點擊事件 alarm_pop_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (null != mOnClickListener) { mOnClickListener.dispose(); } } }); } /** * 顯示彈窗列表界面 */ public void show(View view) { int[] location = new int[2]; view.getLocationOnScreen(location); //Gravity.BOTTOM設(shè)置在view下方,還可以根據(jù)location來設(shè)置PopupWindowj顯示的位置 showAtLocation(view, Gravity.BOTTOM, 0, 0); } /** * @param * @author ldm * @description 點擊事件回調(diào)處理接口 * @time 2016/7/29 15:30 */ public interface IPopuWindowListener { void dispose(); } }
注:設(shè)置PopupWindow的寬高還可以通過LayoutParams來設(shè)置,比如:
//通過LayoutParams來設(shè)置PopupWindow的高度和寬度 ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams)mContentView.getLayoutParams(); lp.widht=400; lp.height = 180; mContentView.setLayoutParams(lp); //但是直接這樣寫獲取到的lp可能為空,我們在獲取一個View的LayoutParams時,通常應(yīng)該這樣寫: //在addOnGlobalLayoutListener監(jiān)聽中來獲取View的LayoutParams mContentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) mContentView.getLayoutParams(); lp.height = 180; lp.width = 400; mContentView.setLayoutParams(lp); } });
在Activity中使用:
private CustomPopup alarmPopup; .... //初始化PopupWindow這里通過數(shù)據(jù)200來設(shè)置PopupWindow高度 alarmPopup=new CustomPopup(getActivity(), ViewGroup.LayoutParams.MATCH_PARENT, 200, this);//這里的this是指當(dāng)前Activity實現(xiàn)了PopupWindow中IPopuWindowListener接口 //彈出PopupWindow @Override protected void widgetClick(View v) { super.widgetClick(v); switch (v.getId()) { case R.id.popup: alarmPopup.show(v); break; } } //處理PopupWindow中按鈕點擊回調(diào) @Override public void dispose() { //TODO sth if (alarmPopup.isShowing()) { alarmPopup.dismiss();//關(guān)閉PopupWindow } }
PopupWindow對應(yīng) 的布局界面:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pop_ll" android:layout_width="match_parent" android:layout_height="60dp" android:background="#404040" android:orientation="horizontal"> <Button android:id="@+id/alarm_pop_btn" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@null" android:gravity="center" android:text="@string/dispose" android:textColor="#FFFFFF" android:textSize="18sp" /> </LinearLayout>
動畫style:
<style name="popwindow_exit_anim_style" parent="android:Animation"> <item name="android:windowEnterAnimation">@anim/popshow_anim</item> <!-- 指定顯示的動畫xml --> <item name="android:windowExitAnimation">@anim/pophidden_anim</item> <!-- 指定消失的動畫xml --> </style>
popshow_anim.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="500" android:fromYDelta="100%p" android:toYDelta="0" /> <alpha android:duration="500" android:fromAlpha="0.0" android:toAlpha="1.0" /> </set>
pophidden_anim.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="500" android:fromYDelta="0" android:toYDelta="50%p" /> <alpha android:duration="500" android:fromAlpha="1.0" android:toAlpha="0.0" /> </set>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Kotlin中ListView與RecyclerView的應(yīng)用講解
這篇文章主要介紹了Kotlin中ListView與RecyclerView的應(yīng)用講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09Android 自定義view實現(xiàn)TopBar效果
這篇文章主要為大家詳細(xì)介紹了Android 自定義view實現(xiàn)TopBar效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09android4.0混淆XmlPullParser報錯原因分析解決
今天,用android4.0在proguard-project.txt中加入 -libraryjars libs/ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar這句話后,混淆時報上面的錯誤,下面與大家分享下具體的解決方法2013-06-06Android中使用socket通信實現(xiàn)消息推送的方法詳解
這篇文章主要介紹了Android中使用socket通信實現(xiàn)消息推送的方法,文中舉了一個消息發(fā)送端和一個消息接收端以及服務(wù)器端的例子來說明原理并且展示了客戶端的實現(xiàn),需要的朋友可以參考下2016-04-04android開發(fā)基礎(chǔ)教程—三種方式實現(xiàn)xml文件解析
本文將介紹三種方式:sax方式/dom方式/pull方式實現(xiàn)xml文件解析,感興趣的朋友可以了解下2013-01-01