Android自定义Dialog实现弹出二维码效果
2016-11-26 23:02
1031 查看
做Android应用中,最缺少不了的就是自定义Dialog,对于系统默认提供的Dialog样式,一般都不复合我们应用的样式。
自定义Dialog需要3步骤即可:
1、主要的重写Dialog的Java类
2、自定义布局文件、并设置Dialog Theme,在style.xml文件中加一个即可
3、使用方法
一、创建CustomPopDialog2.java类
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
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
[/code]
这里简单说明下,我们自定义Dialog需要准备一个自己的View布局文件,主要关注create()方法即可,本例中就是直接显示一个图片。
二、自定义View的布局文件、并在style.xml中添加theme
2
3
4
5
6
7
8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[/code]
2
3
4
5
6
1
2
3
4
5
6
[/code]
三、使用自定义的Dialog
2
3
4
5
6
1
2
3
4
5
6
[/code]
最终效果图:
自定义Dialog需要3步骤即可:
1、主要的重写Dialog的Java类
2、自定义布局文件、并设置Dialog Theme,在style.xml文件中加一个即可
3、使用方法
一、创建CustomPopDialog2.java类
import android.app.Dialog; import android.content.Context; import android.graphics.Bitmap; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager.LayoutParams; import android.widget.ImageView; /** * 该自定义Dialog应用在:弹出框居中显示图片,点击其他区域自动关闭Dialog * */ public class CustomPopDialog2 extends Dialog { public CustomPopDialog2(Context context) { super(context); } public CustomPopDialog2(Context context, int theme) { super(context, theme); } public static class Builder { private Context context; private Bitmap image; public Builder(Context context) { this.context = context; } public Bitmap getImage() { return image; } public void setImage(Bitmap image) { this.image = image; } public CustomPopDialog2 create() { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); final CustomPopDialog2 dialog = new CustomPopDialog2(context,R.style.Dialog); View layout = inflater.inflate(R.layout.dialog_share_qrcode, null); dialog.addContentView(layout, new LayoutParams( android.view.ViewGroup.LayoutParams.WRAP_CONTENT , android.view.ViewGroup.LayoutParams.WRAP_CONTENT)); dialog.setContentView(layout); ImageView img = (ImageView)layout.findViewById(R.id.img_qrcode); img.setImageBitmap(getImage()); return dialog; } } }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
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
[/code]
这里简单说明下,我们自定义Dialog需要准备一个自己的View布局文件,主要关注create()方法即可,本例中就是直接显示一个图片。
二、自定义View的布局文件、并在style.xml中添加theme
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" android:id="@+id/rootLayout"> <ImageView android:id="@+id/img_qrcode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="二维码" /> </LinearLayout>1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[/code]
<style name="Dialog" parent="android:style/Theme.Dialog"> <item name="android:background">#00000000</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> </style>1
2
3
4
5
6
1
2
3
4
5
6
[/code]
三、使用自定义的Dialog
Bitmap bitmap = xxxxx;// 这里是获取图片Bitmap,也可以传入其他参数到Dialog中 CustomPopDialog2.Builder dialogBuild = new CustomPopDialog2.Builder(context); dialogBuild.setImage(bitmap); CustomPopDialog2 dialog = dialogBuild.create(); dialog.setCanceledOnTouchOutside(true);// 点击外部区域关闭 dialog.show();1
2
3
4
5
6
1
2
3
4
5
6
[/code]
最终效果图:
相关文章推荐
- android自定义dialog实现下拉弹出框效果
- 自定义Dialog对话框并实现从底部弹出的动画效果
- Android UI设计系列之自定义Dialog实现各种风格的对话框效果(7)
- 21种ANDROID自定义DIALOG_动画弹出对话框效果组件
- Android实现自定义AlertDialog的自下向上的动画效果(并消除dialog边框)
- Android中Dialog实现自定义弹出对话框
- Android安卓用Dialog对话框简单并且完美实现popupWindow底部弹出效果,有动画效果
- Android自定义Dialog实现加载对话框效果
- Android自定义加载等待Dialog弹窗控件(仿ios效果实现)
- Android自定义加载等待Dialog弹窗控件(仿ios效果实现)
- Android UI设计之<七>自定义Dialog,实现各种风格效果的对话框
- android dialog实现底部弹出和手指滑动隐藏效果
- Android实现自定义AlertDialog的自下向上的动画效果(并消除dialog边框)
- Android自定义popupwindow实现ios中UC浏览器中长按图片弹出的PopupWindow效果
- Android自定义Dialog实现文字动态加载效果
- 自定义Dialog,实现由下而上的弹出效果(模仿QQ退出等)
- Android自定义PopWindow实现QQ、微信弹出菜单效果
- Android自定义弹出框dialog效果
- Android 中从屏幕左下角弹出Dialog动画效果的实现代码
- Android中自定义PopupWindow实现弹出框并带有动画效果