Android第三十三期 - Dialog的应用

简介:

    今天遇到一个大难题哦,不过有大牛一眼就瞄出来了,然后就解决了,AlertDialog和Dialog自定义后圆角的处理,如果你跟我一样没有看到这些细节的话就栽了,用AlertDialog不能使得圆角背景透明化,所以要用Dialog处理才行,也就是下面的方法。

    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
57
58
59
60
61
62
63
64
65
private  static  Dialog mDialog;
 
     // 加载gridview中的item的xml方法
     public  static  View getView(Context context,  int  layoutId) {
         LayoutInflater inflater = (LayoutInflater) context
                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View layout = inflater.inflate(layoutId,  null );
         return  layout;
     }
/**
      * 显示自定义对话框
     
      * @param context
      * @param message
      * @param listener
      */
     public  static  void  showDialog( final  Context context, String message,
             final  IAlertDialogButtonListener listener) {
         View dialogView =  null ;
         // Dialog.Builder builder = new Builder(context,
         // R.style.Theme_Transparent);
         mDialog =  new  Dialog(context, R.style.Theme_Transparent);
 
         dialogView = getView(context, R.layout.dialog_view);
         Button btn_ok = (Button) dialogView.findViewById(R.id.btn_ok);
         Button btn_cancel = (Button) dialogView.findViewById(R.id.btn_cancel);
         TextView txt_dailog_message = (TextView) dialogView
                 .findViewById(R.id.txt_dailog_message);
         txt_dailog_message.setText(message);
         btn_ok.setOnClickListener( new  View.OnClickListener() {
 
             @Override
             public  void  onClick(View arg0) {
                 // 关闭dialog
                 if  (mDialog !=  null ) {
                     mDialog.cancel();
                 }
                 // 事件回调
                 if  (listener !=  null ) {
                     listener.onClick();
                 }
 
                 // 播放音效
                 // MyPlayer.playTone(context, MyPlayer.INDEX_STONE_ENTER);
             }
         });
         btn_cancel.setOnClickListener( new  View.OnClickListener() {
 
             @Override
             public  void  onClick(View arg0) {
                 // 关闭dialog
                 if  (mDialog !=  null ) {
                     mDialog.cancel();
                 }
                 // 播放音效
                 // MyPlayer.playTone(context, MyPlayer.INDEX_STONE_CANCEL);
             }
         });
         // 为dialog设置View
         // builder.setView(dialogView);
         mDialog.setContentView(dialogView);
         // mDialog = builder.create();
         mDialog.show();
     }
}

    AlertDialog:

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
/**
      * 显示自定义对话框
     
      * @param context
      * @param message
      * @param listener
      */
     public  static  void  showDialog( final  Context context, String message,
             final  IAlertDialogButtonListener listener) {
         View dialogView =  null ;
         AlertDialog.Builder builder =  new  Builder(context,
                 R.style.Theme_Transparent);
         dialogView = getView(context, R.layout.dialog_view);
         ImageButton btn_ok = (ImageButton) dialogView.findViewById(R.id.btn_ok);
         ImageButton btn_cancel = (ImageButton) dialogView
                 .findViewById(R.id.btn_cancel);
         TextView txt_dailog_message = (TextView) dialogView
                 .findViewById(R.id.txt_dailog_message);
         txt_dailog_message.setText(message);
         btn_ok.setOnClickListener( new  View.OnClickListener() {
 
             @Override
             public  void  onClick(View arg0) {
                 // 关闭dialog
                 if  (mAlertDialog !=  null ) {
                     mAlertDialog.cancel();
                 }
                 // 事件回调
                 if  (listener !=  null ) {
                     listener.onClick();
                 }
 
                 // 播放音效
                 MyPlayer.playTone(context, MyPlayer.INDEX_STONE_ENTER);
             }
         });
         btn_cancel.setOnClickListener( new  View.OnClickListener() {
 
             @Override
             public  void  onClick(View arg0) {
                 // 关闭dialog
                 if  (mAlertDialog !=  null ) {
                     mAlertDialog.cancel();
                 }
                 // 播放音效
                 MyPlayer.playTone(context, MyPlayer.INDEX_STONE_CANCEL);
             }
         });
         // 为dialog设置View
         builder.setView(dialogView);
         mAlertDialog = builder.create();
         mAlertDialog.show();
     }

    af_dialog_background圆角:

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version= "1.0"  encoding= "utf-8" ?>
<shape xmlns:android= "http://schemas.android.com/apk/res/android"  >
 
     <solid android:color= "#3C4856"  />
 
     <corners
        android:bottomLeftRadius= "0.1dp"
        android:bottomRightRadius= "0.1dp"
         android:topLeftRadius= "10dp"
         android:topRightRadius= "10dp"  />
 
</shape>

    dialog_view:

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "fill_parent"
     android:layout_height= "wrap_content"
     android:gravity= "center_horizontal"
     android:orientation= "vertical"
     android:paddingLeft= "10dp"
     android:paddingRight= "10dp"  >
 
     <LinearLayout
         android:layout_width= "match_parent"
         android:layout_height= "wrap_content"
         android:background= "@drawable/af_dialog_background"
         android:orientation= "vertical"  >
 
         <TextView
             style= "@style/TextViewStyle_aboutus"
             android:layout_gravity= "left"
             android:layout_marginLeft= "10dp"
             android:layout_marginTop= "5dp"
             android:text= "@string/af_logo_10_dialog_title"
             android:textColor= "@color/white4"
             android:textSize= "14sp"  />
 
         <ScrollView
             android:layout_width= "wrap_content"
             android:layout_height= "wrap_content"
             android:layout_marginLeft= "10dp"
             android:layout_marginRight= "10dp"
             android:layout_marginTop= "10dp"
             android:background= "@drawable/af_dialog_background"
             android:fillViewport= "true"
             android:scrollbars= "none"  >
 
             <TextView
                 android:id= "@+id/txt_dailog_message"
                 style= "@style/TextViewStyle_aboutus"
                 android:lineSpacingMultiplier= "1.2"
                 android:text= "@string/af_logo_10_dialog_content"
                 android:textColor= "@color/white4"
                 android:textSize= "14sp"  />
         </ScrollView>
     </LinearLayout>
 
     <LinearLayout
         android:layout_width= "match_parent"
         android:layout_height= "wrap_content"
         android:background= "@color/white"
         android:gravity= "center_horizontal"
         android:orientation= "horizontal"  >
 
         <Button
             android:id= "@+id/btn_cancel"
             style= "?android:attr/buttonBarButtonStyle"
             android:layout_width= "0dp"
             android:layout_height= "wrap_content"
             android:layout_marginBottom= "5dp"
             android:layout_marginLeft= "25dp"
             android:layout_marginRight= "15dp"
             android:layout_marginTop= "5dp"
             android:layout_weight= "1"
             android:background= "@drawable/btn_cancelclick"
             android:contentDescription= "@string/action_settings"
             android:paddingBottom= "8dp"
             android:paddingTop= "8dp"
             android:text= "@string/af_cancel"
             android:textColor= "@color/white"
             android:textSize= "16sp"  />
 
         <Button
             android:id= "@+id/btn_ok"
             style= "?android:attr/buttonBarButtonStyle"
             android:layout_width= "0dp"
             android:layout_height= "wrap_content"
             android:layout_marginBottom= "5dp"
             android:layout_marginLeft= "15dp"
             android:layout_marginRight= "25dp"
             android:layout_marginTop= "5dp"
             android:layout_weight= "1"
             android:background= "@drawable/btn_able"
             android:contentDescription= "@string/action_settings"
             android:paddingBottom= "8dp"
             android:paddingTop= "8dp"
             android:text= "@string/af_confirm"
             android:textColor= "@color/white"
             android:textSize= "16sp"  />
     </LinearLayout>
 
</LinearLayout>

    最后是style:

1
2
3
4
5
6
7
8
9
10
<style name= "Theme_Transparent"  parent= "@android:style/Theme.Dialog" >
         <item name= "android:windowFrame" > @null </item>
         <item name= "android:windowIsFloating" > true </item>
         <item name= "android:windowIsTranslucent" > true </item>
         <item name= "android:windowNoTitle" > true </item>
         <item name= "android:background" > @android :color/transparent</item>
         <item name= "android:windowBackground" > @android :color/transparent</item>
         <item name= "android:backgroundDimEnabled" > true </item>
         <item name= "android:backgroundDimAmount" > 0.6 </item>
     </style>

    谢谢hongyang大神~~开心挣钱每一天









本文转自 吴雨声 51CTO博客,原文链接:http://blog.51cto.com/liangxiao/1613291,如需转载请自行联系原作者
目录
相关文章
|
10月前
|
开发框架 前端开发 Android开发
Flutter 与原生模块(Android 和 iOS)之间的通信机制,包括方法调用、事件传递等,分析了通信的必要性、主要方式、数据传递、性能优化及错误处理,并通过实际案例展示了其应用效果,展望了未来的发展趋势
本文深入探讨了 Flutter 与原生模块(Android 和 iOS)之间的通信机制,包括方法调用、事件传递等,分析了通信的必要性、主要方式、数据传递、性能优化及错误处理,并通过实际案例展示了其应用效果,展望了未来的发展趋势。这对于实现高效的跨平台移动应用开发具有重要指导意义。
966 4
|
5月前
|
存储 Android开发
如何查看Flutter应用在Android设备上已被撤销的权限?
如何查看Flutter应用在Android设备上已被撤销的权限?
255 64
|
4月前
|
Android开发 开发者
Android中Dialog位置+样式的设置
本文介绍了在Android开发中如何设置Dialog的位置和样式。通过自定义`MyDialog`类,可以灵活调整Dialog的显示位置,例如将其固定在屏幕底部,并设置宽度匹配父布局。同时,文章还展示了如何模仿Android原生Dialog样式,通过定义`MyDialogStyle`去除标题栏、设置背景透明度、添加阴影效果以及配置点击外部关闭等功能,从而实现更加美观和符合需求的Dialog效果。代码示例详细,便于开发者快速上手实现。
305 2
|
7月前
|
前端开发 Java Shell
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
416 20
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
7月前
|
Dart 前端开发 Android开发
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
166 4
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
10月前
|
算法 Java 数据库
Android 应用的主线程在什么情况下会被阻塞?
【10月更文挑战第20天】为了避免主线程阻塞,我们需要合理地设计和优化应用的代码。将耗时操作移到后台线程执行,使用异步任务、线程池等技术来提高应用的并发处理能力。同时,要注意避免出现死循环、不合理的锁使用等问题。通过这些措施,可以确保主线程能够高效地运行,提供流畅的用户体验。
465 58
|
9月前
|
JSON Java API
探索安卓开发:打造你的首个天气应用
在这篇技术指南中,我们将一起潜入安卓开发的海洋,学习如何从零开始构建一个简单的天气应用。通过这个实践项目,你将掌握安卓开发的核心概念、界面设计、网络编程以及数据解析等技能。无论你是初学者还是有一定基础的开发者,这篇文章都将为你提供一个清晰的路线图和实用的代码示例,帮助你在安卓开发的道路上迈出坚实的一步。让我们一起开始这段旅程,打造属于你自己的第一个安卓应用吧!
243 14
|
9月前
|
Java Linux 数据库
探索安卓开发:打造你的第一款应用
在数字时代的浪潮中,每个人都有机会成为创意的实现者。本文将带你走进安卓开发的奇妙世界,通过浅显易懂的语言和实际代码示例,引导你从零开始构建自己的第一款安卓应用。无论你是编程新手还是希望拓展技术的开发者,这篇文章都将为你打开一扇门,让你的创意和技术一起飞扬。
179 13
|
9月前
|
搜索推荐 前端开发 测试技术
打造个性化安卓应用:从设计到开发的全面指南
在这个数字时代,拥有一个定制的移动应用不仅是一种趋势,更是个人或企业品牌的重要延伸。本文将引导你通过一系列简单易懂的步骤,从构思你的应用理念开始,直至实现一个功能齐全的安卓应用。无论你是编程新手还是希望拓展技能的开发者,这篇文章都将为你提供必要的工具和知识,帮助你将创意转化为现实。
|
10月前
|
JSON Java Android开发
探索安卓开发之旅:打造你的第一个天气应用
【10月更文挑战第30天】在这个数字时代,掌握移动应用开发技能无疑是进入IT行业的敲门砖。本文将引导你开启安卓开发的奇妙之旅,通过构建一个简易的天气应用来实践你的编程技能。无论你是初学者还是有一定经验的开发者,这篇文章都将成为你宝贵的学习资源。我们将一步步地深入到安卓开发的世界中,从搭建开发环境到实现核心功能,每个环节都充满了发现和创造的乐趣。让我们开始吧,一起在代码的海洋中航行!