国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android自定義PopupWindow實(shí)現(xiàn)炫酷的IOS對話框效果

2019-10-22 18:11:29
字體:
供稿:網(wǎng)友

前言:

最近在使用IOS系統(tǒng)的過程中發(fā)現(xiàn)IOS底部彈出框甚是漂亮,大氣,上檔次,于是乎就想啊能不能在Android中實(shí)現(xiàn)類似的對話框呢?你說,這不是廢話嗎,除了一些極少數(shù)的系統(tǒng)級的不能模仿外(版權(quán))還有啥不能依瓢畫葫蘆的呢,所以啊,這篇文章將介紹如何在Android中實(shí)現(xiàn)高仿IOS對話框效果,先上圖,給大家養(yǎng)養(yǎng)眼:

android,自定義,popupwindow,ios,對話框

大家在看到上面的對話框時有沒有想到簡單的實(shí)現(xiàn)思路呢?我這里給出的思路是我們可以ios/248157.html">自定義一個PopupWindow,然后設(shè)置我們的布局。這里的布局很有技巧哦,那就是對話框中間的透明隔斷區(qū)域其實(shí)是一個margin值,每個隔斷的item layout的背景為一個白色圓角矩形,之后再讓PopupWindow的背景為透明即可,是不是很簡單呢。好了,讓我們動手編寫代碼將它帶回家吧。

大家也可以看看我的上篇文章:Android自定義Dialog,炫酷主流的加載對話框。

代碼實(shí)現(xiàn)

1. 編寫布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android/223401.html">android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="match_parent"  android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:paddingTop="12dp" android:paddingBottom="12dp" android:background="@drawable/corner_white_bg" android:layout_height="wrap_content"> <Button  android:id="@id/btn_share_weixin"  android:layout_width="0dp"  android:layout_weight="1"  android:drawableTop="@mipmap/ic_share_weixin"  android:drawablePadding="6dp"  android:background="@null"  android:textColor="@android:color/black"  android:textSize="@dimen/text_14_sp"  android:text="@string/weixin"  android:layout_height="wrap_content"/> <Button  android:id="@id/btn_share_friends"  android:layout_width="0dp"  android:layout_weight="1"  android:drawableTop="@mipmap/ic_share_friends"  android:drawablePadding="6dp"  android:background="@null"  android:textColor="@android:color/black"  android:textSize="@dimen/text_14_sp"  android:text="@string/weixin_friends"  android:layout_height="wrap_content"/> <Button  android:id="@id/btn_share_qq"  android:layout_width="0dp"  android:layout_weight="1"  android:drawableTop="@mipmap/ic_share_qq"  android:drawablePadding="6dp"  android:background="@null"  android:textColor="@android:color/black"  android:textSize="@dimen/text_14_sp"  android:text="@string/qq"  android:layout_height="wrap_content"/> <Button  android:id="@id/btn_share_qq_zone"  android:layout_width="0dp"  android:layout_weight="1"  android:drawableTop="@mipmap/ic_share_zones"  android:drawablePadding="6dp"  android:textColor="@android:color/black"  android:textSize="@dimen/text_14_sp"  android:background="@null"  android:text="@string/qq_zones"  android:layout_height="wrap_content"/> </LinearLayout> <Button android:id="@id/btn_cancel" android:layout_width="match_parent" android:textColor="@android:color/black" android:textSize="@dimen/text_14_sp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:text="@string/cancel" android:background="@drawable/corner_white_bg" android:layout_height="wrap_content"/></LinearLayout>

這里被隔斷的部分有兩個,所以布局中有兩個view的背景為白色圓角矩形。

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="10dp"/> <solid android:color="@android:color/white"/></shape>

2. 繼承PopupWindow

public class IosPopupWindow extends PopupWindow implements View.OnClickListener { private Context mContext; public IosPopupWindow(Activity activity) { super(activity); mContext = activity; LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View contentView = inflater.inflate(R.layout.dialog_share, null); setContentView(contentView); int screenWidth = activity.getWindowManager().getDefaultDisplay().getWidth(); //獲取popupwindow的高度與寬度 this.setWidth((int) (screenWidth - 2 * dp2px(mContext,12f))); this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); // 設(shè)置背景透明度 setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 設(shè)置動畫 this.setAnimationStyle(R.style.IosDialog); // 設(shè)置彈出窗體可點(diǎn)擊 this.setFocusable(true); // 點(diǎn)擊外部可取消 this.setOutsideTouchable(true); initView(contentView); }

以上代碼最關(guān)鍵的就是給我們的PopupWindow設(shè)置一個透明的背景Drawable啦。

3. 窗口彈出時讓外部變暗

/** * 讓popupwindow以外區(qū)域陰影顯示 */private void popOutShadow() { final Window window = ((Activity) mContext).getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); lp.alpha = 0.5f;//設(shè)置陰影透明度 window.setAttributes(lp); setOnDismissListener(new OnDismissListener() { @Override public void onDismiss() {  WindowManager.LayoutParams lp = window.getAttributes();  lp.alpha = 1f;  window.setAttributes(lp); } });}

與Dialog不同的是PopupWindow實(shí)現(xiàn)外部變暗需通過改變它依附的window的透明度,所以我們傳給PopupWindow的Context需為Activity類型,同時在窗口消失的時候記得將Window的透明度重置。

最后,奉上IosPopupWindow的github,你值得擁有:https://github.com/ydxlt/LoadingDialog

總結(jié)

以上所述是小編給大家介紹的Android自定義PopupWindow實(shí)現(xiàn)炫酷的IOS對話框效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對VEVB武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 达拉特旗| 紫云| 萝北县| 桑日县| 顺平县| 平山县| 宁明县| 尚义县| 建德市| 镇原县| 会泽县| 贺州市| 息烽县| 东源县| 通州市| 上高县| 贵南县| 来凤县| 合江县| 柘荣县| 松溪县| 通榆县| 长兴县| 工布江达县| 汝南县| 浠水县| 拜泉县| 和林格尔县| 西乌珠穆沁旗| 吴桥县| 富裕县| 银川市| 托里县| 醴陵市| 林州市| 常宁市| 三江| 湟源县| 龙游县| 德江县| 萝北县|