您现在的位置是:首页 > 文章详情

android自定义相对复杂dialog

日期:2018-07-26点击:378

之前写了两篇关于Dialog的算是文章吧(2333),分别是:
1、 Android 系统原生dialog使用
2、 Android dialog Activity 使用
下面开始第三篇:相对复杂的自定义dialog。

一、转圈加载 dialog

见过大佬自己自定义的的 dialog ,也就是自己画出来的,额,楼主比较渣、楼主搞不了、我比较怂(一看就是有自知之明的好楼主)。但是呢,我还不服输,于是楼主用了偷懒的方法,废话不说,开干:

1、画个布局 即 layout.xml 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" xmlns:tools="http://schemas.android.com/tools" android:background="@drawable/shape_wait_dialog" android:gravity="center" android:orientation="vertical" android:padding="20dp"> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminateDrawable="@drawable/anim_dialog_wait" /> <TextView android:id="@+id/tv_waitDialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:singleLine="true" tools:text="请稍等,拼命中..." android:textColor="#1296DB" android:textSize="14sp" /> </LinearLayout> 

这里有一个图标:

加载图标

对了,告诉各位一个很好的图标网站哈,不知道小伙伴一定要收藏,如果你家ui不在你可以自己去搞:
阿里妈妈 MUX

2、图片的旋转动画

在 drawable 文件夹下创建一个动画文件 当然了你也可以去 amin 文件夹下搞

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/ic_wait_dialog" android:pivotX="50%" android:pivotY="50%" /> 

简单的动画没啥好说的

3、设置 dialog的小背景

在 drawable 文件夹下创建一个 shape 文件

<shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="10dp" /> <!--不同颜色不同效果哦--> <solid android:color="#3c3c3c" /> </shape> 

4、很关键的一步 设置 style

在 value/style.xml 文件下设置 我们的 dialog 的基本属性

 <style name="loading_dialog_style" parent="@android:style/Theme.Dialog"> <!--提示框是否有边框--> <item name="android:windowFrame">@null</item> <!--是否为浮动窗口--> <item name="android:windowIsFloating">true</item> <!--是否半透明--> <item name="android:windowIsTranslucent">false</item> <!--去除title--> <item name="android:windowNoTitle">true</item> <!--窗口背景颜色--> <item name="android:windowBackground">@drawable/shape_wait_dialog</item> <!--是否允许对话框的背景变暗--> <item name="android:backgroundDimEnabled">true</item> <!--控制灰度值--> <item name="android:backgroundDimAmount">0.4</item> </style> 

5、在使用

 private Dialog myDialog; /** * 展示 dialog * * @param context 上下文 * @param waitContent 展示文字 * @param canCancel 是否点击 dialog 周围关闭,是否可以返回键关闭 true 表示可以, false 你懂得 */ private void showMyWait(Context context, String waitContent, boolean canCancel) { @SuppressLint("InflateParams") View view = LayoutInflater.from(this).inflate(R.layout.layout_wait_dialog, null); TextView tvWaitDialog = view.findViewById(R.id.tv_waitDialog); tvWaitDialog.setText(waitContent); myDialog = new Dialog(context, R.style.loading_dialog_style); myDialog.setCancelable(canCancel); myDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); myDialog.show(); } /** * 主动关闭 dialog */ private void cancelDialog() { if (myDialog != null && myDialog.isShowing()) { myDialog.cancel(); } } 

原文链接:https://yq.aliyun.com/articles/632783
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章