一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Android - Android自定義PopupWindow實(shí)現(xiàn)炫酷的IOS對話框效果

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

2022-02-19 16:42宿罪 Android

這篇文章主要給大家介紹如何在android中實(shí)現(xiàn)高仿ios對話框效果,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

前言:

最近在使用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實(shí)現(xiàn)炫酷的IOS對話框效果

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

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

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

1. 編寫布局

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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>

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

?
1
2
3
4
5
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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è)置動(dòng)畫
 this.setAnimationStyle(R.style.IosDialog);
 // 設(shè)置彈出窗體可點(diǎn)擊
 this.setFocusable(true);
 // 點(diǎn)擊外部可取消
 this.setOutsideTouchable(true);
 initView(contentView);
 }

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

3. 窗口彈出時(shí)讓外部變暗

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * 讓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類型,同時(shí)在窗口消失的時(shí)候記得將Window的透明度重置。

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

總結(jié)

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

原文鏈接:https://blog.csdn.net/ydxlt/article/details/80290053

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: jux539原千岁在线播放 | 久久精品无码一区二区日韩av | 青青青视频蜜桃一区二区 | 午夜欧美福利视频 | 69萝莉| 秋葵丝瓜茄子草莓榴莲樱桃 | 幸福草电视剧演员表介绍 | 欧洲肥女大肥臀 | 久久re6热在线视频 久久AV喷吹AV高潮欧美 | 欧美色图亚洲天堂 | 亚洲精品在线播放 | 91私密保健女子养生spa | 亚洲欧美影院 | 国产精品对白刺激久久久 | 日本艳鉧动漫1~6在线观看 | 亚洲成色WWW久久网站夜月 | 日韩资源| 久久99r66热这里只有精品 | 亚洲色图中文字幕 | 亚洲精品国产专区91在线 | 美女把腿开让我 | 天堂资源在线8 | 香蕉久久一区二区三区啪啪 | 操操综合网| 丝瓜视频成人在线观看 | 国产一区二区三区高清 | 任我鲁精品视频精品 | 亚洲国产精品无码中文在线 | 四虎永久免费在线观看 | 国色天香社区在线 | 久久精品国产只有精品 | 波多野结衣伦理在线观看 | 国产亚洲综合精品一区二区三区 | 国产精品区牛牛影院 | gaygayas男男免费中国 | 国产一区二区免费不卡在线播放 | 情缘1完整版在线观看 | juliaann主妇疯狂| 精品国产一区二区三区久久久蜜臀 | 亚洲欧美精品久久 | freexxxx性护士第一次 |