Android實(shí)現(xiàn)簡(jiǎn)單的popupwindow提示框
Popupwindow大家肯定都特別熟悉了 像一般的提示框的話(huà)我們會(huì)用Dialog來(lái)做 但是隨著設(shè)計(jì)要求的不斷提高,App中各式各樣的提示框都有,很明顯普通的Dialog實(shí)現(xiàn)起來(lái)就比較吃力了 所以用Popupwindow來(lái)實(shí)現(xiàn)是最好不過(guò)了 ,于是我也自己寫(xiě)了一個(gè)popupwindow彈出的一個(gè)方法,代碼量少簡(jiǎn)單靈活 先看一下效果圖

大致效果就是這樣 當(dāng)然你也可以將layout中的布局換成自己的布局 接下來(lái)是代碼
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)聽(tīng)
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)用的方法 背景變暗可以通過(guò)這段代碼來(lái)實(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="請(qǐng)選擇分享平臺(tái)"
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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中仿IOS提示框的實(shí)現(xiàn)方法
- Android使用Toast顯示消息提示框
- IOS 仿Android吐司提示框的實(shí)例(分享)
- Android 自定義一套 Dialog通用提示框 (代碼庫(kù))
- Android仿IOS自定義AlertDialog提示框
- Android仿QQ、微信聊天界面長(zhǎng)按提示框效果
- Android仿百度谷歌搜索自動(dòng)提示框AutoCompleteTextView簡(jiǎn)單應(yīng)用示例
- Android超實(shí)用的Toast提示框優(yōu)化分享
- Android實(shí)現(xiàn)Toast提示框圖文并存的方法
- Android編程之自定義AlertDialog(退出提示框)用法實(shí)例
- Android模擬美團(tuán)客戶(hù)端進(jìn)度提示框
- android 彈出提示框的使用(圖文實(shí)例)
- android實(shí)現(xiàn)彈出提示框
相關(guān)文章
Android WebViewClient 的 `shouldOverrideUrlLoa
這篇文章主要介紹了Android WebViewClient 的 shouldOverrideUrlLoading方法,了解并正確實(shí)現(xiàn) WebViewClient 中的 shouldOverrideUrlLoading 方法對(duì)于在你的 Android 應(yīng)用中提供順暢且安全的瀏覽體驗(yàn)至關(guān)重要,需要的朋友可以參考下2024-07-07
Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
深入了解Android中GestureDetector的定義與使用
Android中的GestureDetector?可以使用?MotionEvents?檢測(cè)各種手勢(shì)和事件,非常的好用。本文將會(huì)通過(guò)幾個(gè)具體的例子來(lái)講解一下GestureDetector的具體使用方法,需要的可以參考一下2023-01-01
unity3d發(fā)布apk在android虛擬機(jī)中運(yùn)行的詳細(xì)步驟(unity3d導(dǎo)出android apk)
這篇文章主要介紹了unity3d發(fā)布apk在android虛擬機(jī)中運(yùn)行的詳細(xì)步驟,需要的朋友可以參考下2014-05-05
Android應(yīng)用圖標(biāo)在狀態(tài)欄上顯示實(shí)現(xiàn)原理
Android應(yīng)用圖標(biāo)在狀態(tài)欄上顯示,以及顯示不同的圖標(biāo),其實(shí)很研究完后,才發(fā)現(xiàn),很簡(jiǎn)單,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06
Android自定義控件LinearLayout實(shí)例講解
這篇文章主要為大家詳細(xì)介紹了Android自定義控件LinearLayout實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05

