Android實(shí)現(xiàn)簡單的popupwindow提示框
Popupwindow大家肯定都特別熟悉了 像一般的提示框的話我們會用Dialog來做 但是隨著設(shè)計(jì)要求的不斷提高,App中各式各樣的提示框都有,很明顯普通的Dialog實(shí)現(xiàn)起來就比較吃力了 所以用Popupwindow來實(shí)現(xiàn)是最好不過了 ,于是我也自己寫了一個(gè)popupwindow彈出的一個(gè)方法,代碼量少簡單靈活 先看一下效果圖
大致效果就是這樣 當(dāng)然你也可以將layout中的布局換成自己的布局 接下來是代碼
private void ejectPopup() { View parent = ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0); View popView = View.inflate(this, R.layout.details_share, null); int width = getResources().getDisplayMetrics().widthPixels; int height = getResources().getDisplayMetrics().heightPixels; // int i = height /5*2; popWindow = new PopupWindow(popView, width, ViewGroup.LayoutParams.WRAP_CONTENT); popWindow.setAnimationStyle(R.style.Search_PopupWindowAnimation); popWindow.setFocusable(true); popWindow.setOutsideTouchable(false);// 設(shè)置同意在外點(diǎn)擊消失 ColorDrawable dw = new ColorDrawable(0x30000000); popWindow.setBackgroundDrawable(dw); popWindow.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);//被home鍵擋住 //給popup中的按鈕做監(jiān)聽 WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = (float) 0.7; //0.0-1.0 getWindow().setAttributes(lp); popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = (float) 1; //0.0-1.0 getWindow().setAttributes(lp); } }); }
這個(gè)就是調(diào)用的方法 背景變暗可以通過這段代碼來實(shí)現(xiàn)
popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = (float) 1; //0.0-1.0 getWindow().setAttributes(lp); } });
當(dāng)讓也可以讓ui妹子給你切一個(gè)透明的背景圖片
最后是layout中的代碼
<?xml version="1.0" encoding="utf-8"?> <com.zhy.autolayout.AutoLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:background="#fff" android:layout_height="239dp"> <com.zhy.autolayout.AutoLinearLayout android:gravity="center" android:layout_width="match_parent" android:layout_height="51dp"> <TextView android:text="請選擇分享平臺" android:textColor="#29292a" android:textSize="18sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </com.zhy.autolayout.AutoLinearLayout> <TextView android:background="@color/divider_color" android:layout_width="match_parent" android:layout_height="1dp" /> <com.zhy.autolayout.AutoLinearLayout android:layout_marginBottom="10dp" android:layout_width="match_parent" android:layout_height="132dp"> <com.zhy.autolayout.AutoRelativeLayout android:id="@+id/share_WX" android:layout_marginLeft="13dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"> <ImageView android:id="@+id/share_WX_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" app:srcCompat="@drawable/wechat" /> <TextView android:text="微信" android:layout_marginTop="6dp" android:layout_below="@id/share_WX_icon" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </com.zhy.autolayout.AutoRelativeLayout> <com.zhy.autolayout.AutoRelativeLayout android:id="@+id/share_WXPYQ" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"> <ImageView android:id="@+id/share_WXPYQ_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" app:srcCompat="@drawable/circleoffriends" /> <TextView android:text="朋友圈" android:layout_marginTop="6dp" android:layout_below="@id/share_WXPYQ_icon" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </com.zhy.autolayout.AutoRelativeLayout> <com.zhy.autolayout.AutoRelativeLayout android:layout_width="0dp" android:id="@+id/share_QQ" android:layout_weight="1" android:layout_height="match_parent"> <ImageView android:id="@+id/share_QQ_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" app:srcCompat="@drawable/qq" /> <TextView android:text="QQ" android:layout_marginTop="6dp" android:layout_below="@id/share_QQ_icon" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </com.zhy.autolayout.AutoRelativeLayout> <com.zhy.autolayout.AutoRelativeLayout android:layout_width="0dp" android:layout_weight="1" android:id="@+id/share_QQKJ" android:layout_height="match_parent"> <ImageView android:id="@+id/share_QQKJ_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" app:srcCompat="@drawable/zone" /> <TextView android:text="空間" android:layout_marginTop="6dp" android:layout_below="@id/share_QQKJ_icon" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </com.zhy.autolayout.AutoRelativeLayout> <com.zhy.autolayout.AutoRelativeLayout android:id="@+id/share_WB" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginRight="13dp" android:layout_weight="1"> <ImageView android:id="@+id/share_WB_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" app:srcCompat="@drawable/weibo" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/share_WB_icon" android:layout_centerHorizontal="true" android:layout_marginTop="6dp" android:text="微博" /> </com.zhy.autolayout.AutoRelativeLayout> </com.zhy.autolayout.AutoLinearLayout> <TextView android:background="@color/divider_color" android:layout_width="match_parent" android:layout_height="1dp" /> <com.zhy.autolayout.AutoLinearLayout android:id="@+id/share_cancel" android:gravity="center" android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:gravity="center" android:textSize="15sp" android:textColor="#2d2d2d" android:text="取消" android:layout_width="wrap_content" android:layout_height="match_parent" /> </com.zhy.autolayout.AutoLinearLayout> </com.zhy.autolayout.AutoLinearLayout>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中仿IOS提示框的實(shí)現(xiàn)方法
- Android使用Toast顯示消息提示框
- IOS 仿Android吐司提示框的實(shí)例(分享)
- Android 自定義一套 Dialog通用提示框 (代碼庫)
- Android仿IOS自定義AlertDialog提示框
- Android仿QQ、微信聊天界面長按提示框效果
- Android仿百度谷歌搜索自動提示框AutoCompleteTextView簡單應(yīng)用示例
- Android超實(shí)用的Toast提示框優(yōu)化分享
- Android實(shí)現(xiàn)Toast提示框圖文并存的方法
- Android編程之自定義AlertDialog(退出提示框)用法實(shí)例
- Android模擬美團(tuán)客戶端進(jìn)度提示框
- android 彈出提示框的使用(圖文實(shí)例)
- android實(shí)現(xiàn)彈出提示框
相關(guān)文章
Android通過自定義Activity實(shí)現(xiàn)懸浮的Dialog詳解
這篇文章主要給大家介紹了關(guān)于Android通過自定義Activity實(shí)現(xiàn)懸浮的Dialog的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對大家具有一定的參考學(xué)習(xí)價(jià)值,感興趣的朋友們下面來一起看看吧。2017-05-05android LabelView實(shí)現(xiàn)標(biāo)簽云效果
這篇文章主要為大家詳細(xì)介紹了android LabelView實(shí)現(xiàn)標(biāo)簽云效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android 軟鍵盤彈出隱藏?cái)D壓界面等各種問題小結(jié)
這篇文章主要介紹了Android 軟鍵盤彈出隱藏?cái)D壓界面等各種問題的相關(guān)知識,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-11-11Android自定義View實(shí)現(xiàn)開關(guān)按鈕
android 自定義view知識非常廣泛,難以讓人掌握。但是也是andoroid進(jìn)階學(xué)習(xí)的必經(jīng)之路。下面通過本文給大家介紹Android自定義View實(shí)現(xiàn)開關(guān)按鈕的知識,非常不錯,感興趣的朋友一起看看吧2016-11-11Android 單例模式的四種實(shí)現(xiàn)方式
單例模式作為設(shè)計(jì)模式之一,使用場景非常多。本文講述了Android實(shí)現(xiàn)單例模式的幾種方式2021-05-05Android Webview添加網(wǎng)頁加載進(jìn)度條實(shí)例詳解
這篇文章主要介紹了Android Webview添加網(wǎng)頁加載進(jìn)度條實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-01-01Android滑動到頂部和底部時(shí)出現(xiàn)的陰影如何去掉
本文給大家介紹android滑動到頂部和底部時(shí)出現(xiàn)的陰影去掉的解決方法,本文還涉及到listview各個(gè)屬性的用法介紹,非常不錯,具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-10-10Android 應(yīng)用中跳轉(zhuǎn)到應(yīng)用市場評分示例
本篇文章主要介紹了Android 應(yīng)用中跳轉(zhuǎn)到應(yīng)用市場評分示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02Android使用Jetpack Compose開發(fā)零基礎(chǔ)起步教程
Jetpack Compose是用于構(gòu)建原生Android UI的現(xiàn)代工具包。Jetpack Compose使用更少的代碼,強(qiáng)大的工具和直觀的Kotlin API,簡化并加速了Android上的UI開發(fā)2023-04-04