android自定义弹出框样式的实现方法
程序员文章站
2022-09-06 12:29:45
前言:做项目时,感觉android自带的弹出框样式比较丑,很多应用都是自己做的弹出框,这里也试着自己做了一个。废话不说先上图片:实现机制1.先自定义一个弹出框的样式2.自己实现customdialog...
前言:
做项目时,感觉android自带的弹出框样式比较丑,很多应用都是自己做的弹出框,这里也试着自己做了一个。
废话不说先上图片:
实现机制
1.先自定义一个弹出框的样式
2.自己实现customdialog类,继承自dialog,实现里面方法,在里面加载自定义样式的弹出框;
3.使用时,与使用dialog一样
具体代码
dialog_normal_layout.xml样式文件
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:orientation="vertical" android:padding="20.0dip" > <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/bg_bombbox" android:orientation="vertical" > <textview android:id="@+id/title" style="@style/text_18_ffffff" android:layout_width="fill_parent" android:layout_height="40.0dip" android:gravity="center" android:text="@string/title_alert" android:visibility="visible" /> <linearlayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" > <textview android:id="@+id/message" style="@style/text_16_666666" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left|center" android:linespacingmultiplier="1.5" android:minheight="120.0dip" android:paddingbottom="15.0dip" android:paddingleft="20.0dip" android:paddingright="20.0dip" android:paddingtop="15.0dip" /> </linearlayout> <view android:layout_width="fill_parent" android:layout_height="1.0px" android:background="#ffd0d0d0" /> <linearlayout android:layout_width="fill_parent" android:layout_height="60.0dip" android:layout_gravity="bottom" android:background="@drawable/dialog_bottom_bg" android:gravity="center" android:orientation="horizontal" > <button android:id="@+id/positivebutton" style="@style/text_15_ffffff_sdw" android:layout_width="114.0dip" android:layout_height="40.0dip" android:background="@drawable/btn_ok_selector" android:gravity="center" android:text="@string/ok" /> <button android:id="@+id/negativebutton" style="@style/text_15_666666_sdw" android:layout_width="114.0dip" android:layout_height="40.0dip" android:layout_marginleft="20.0dip" android:background="@drawable/btn_cancel_selector" android:gravity="center" android:text="@string/cancel" /> </linearlayout> </linearlayout> </framelayout>
其中引用的样式文件styles.xml
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="appbasetheme" parent="android:theme.light"></style> <style name="apptheme" parent="appbasetheme"></style> <style name="text_18_ffffff"> <item name="android:textsize">18.0dip</item> <item name="android:textcolor">#ffffffff</item> </style> <style name="text_16_666666"> <item name="android:textsize">16.0dip</item> <item name="android:textcolor">#ff666666</item> </style> <style name="sdw_white"> <item name="android:shadowcolor">#7fffffff</item> <item name="android:shadowdx">0.0</item> <item name="android:shadowdy">0.65</item> <item name="android:shadowradius">1.0</item> </style> <style name="sdw_79351b"> <item name="android:shadowcolor">#ff79351b</item> <item name="android:shadowdx">0.0</item> <item name="android:shadowdy">1.0</item> <item name="android:shadowradius">1.0</item> </style> <style name="text_15_ffffff_sdw" parent="@style/sdw_79351b"> <item name="android:textsize">15.0dip</item> <item name="android:textcolor">#ffffffff</item> </style> <style name="text_15_666666_sdw" parent="@style/sdw_white"> <item name="android:textsize">15.0dip</item> <item name="android:textcolor">#ff666666</item> </style> <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> </resources>
自定义dialog的实现类customdialog
package com.dyr.custom; import android.app.dialog; import android.content.context; import android.content.dialoginterface; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup.layoutparams; import android.widget.button; import android.widget.linearlayout; import android.widget.textview; import com.dyr.view.r; public class customdialog extends dialog { public customdialog(context context) { super(context); } public customdialog(context context, int theme) { super(context, theme); } public static class builder { private context context; private string title; private string message; private string positivebuttontext; private string negativebuttontext; private view contentview; private dialoginterface.onclicklistener positivebuttonclicklistener; private dialoginterface.onclicklistener negativebuttonclicklistener; public builder(context context) { this.context = context; } public builder setmessage(string message) { this.message = message; return this; } /** * set the dialog message from resource * * @param title * @return */ public builder setmessage(int message) { this.message = (string) context.gettext(message); return this; } /** * set the dialog title from resource * * @param title * @return */ public builder settitle(int title) { this.title = (string) context.gettext(title); return this; } /** * set the dialog title from string * * @param title * @return */ public builder settitle(string title) { this.title = title; return this; } public builder setcontentview(view v) { this.contentview = v; return this; } /** * set the positive button resource and it's listener * * @param positivebuttontext * @return */ public builder setpositivebutton(int positivebuttontext, dialoginterface.onclicklistener listener) { this.positivebuttontext = (string) context .gettext(positivebuttontext); this.positivebuttonclicklistener = listener; return this; } public builder setpositivebutton(string positivebuttontext, dialoginterface.onclicklistener listener) { this.positivebuttontext = positivebuttontext; this.positivebuttonclicklistener = listener; return this; } public builder setnegativebutton(int negativebuttontext, dialoginterface.onclicklistener listener) { this.negativebuttontext = (string) context .gettext(negativebuttontext); this.negativebuttonclicklistener = listener; return this; } public builder setnegativebutton(string negativebuttontext, dialoginterface.onclicklistener listener) { this.negativebuttontext = negativebuttontext; this.negativebuttonclicklistener = listener; return this; } public customdialog create() { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); // instantiate the dialog with the custom theme final customdialog dialog = new customdialog(context,r.style.dialog); view layout = inflater.inflate(r.layout.dialog_normal_layout, null); dialog.addcontentview(layout, new layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); // set the dialog title ((textview) layout.findviewbyid(r.id.title)).settext(title); // set the confirm button if (positivebuttontext != null) { ((button) layout.findviewbyid(r.id.positivebutton)) .settext(positivebuttontext); if (positivebuttonclicklistener != null) { ((button) layout.findviewbyid(r.id.positivebutton)) .setonclicklistener(new view.onclicklistener() { public void onclick(view v) { positivebuttonclicklistener.onclick(dialog, dialoginterface.button_positive); } }); } } else { // if no confirm button just set the visibility to gone layout.findviewbyid(r.id.positivebutton).setvisibility( view.gone); } // set the cancel button if (negativebuttontext != null) { ((button) layout.findviewbyid(r.id.negativebutton)) .settext(negativebuttontext); if (negativebuttonclicklistener != null) { ((button) layout.findviewbyid(r.id.negativebutton)) .setonclicklistener(new view.onclicklistener() { public void onclick(view v) { negativebuttonclicklistener.onclick(dialog, dialoginterface.button_negative); } }); } } else { // if no confirm button just set the visibility to gone layout.findviewbyid(r.id.negativebutton).setvisibility( view.gone); } // set the content message if (message != null) { ((textview) layout.findviewbyid(r.id.message)).settext(message); } else if (contentview != null) { // if no message set // add the contentview to the dialog body ((linearlayout) layout.findviewbyid(r.id.content)) .removeallviews(); ((linearlayout) layout.findviewbyid(r.id.content)) .addview(contentview, new layoutparams(layoutparams.fill_parent,layoutparams.fill_parent)); } dialog.setcontentview(layout); return dialog; } } }
使用代码
customdialog.builder builder = new customdialog.builder(this); builder.setmessage("这个就是自定义的提示框"); builder.settitle("提示"); builder.setpositivebutton("确定", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); //设置你的操作事项 } }); builder.setnegativebutton("取消", new android.content.dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); } }); builder.create().show();
至此,自定义弹出框已经完成,是不是感觉很简单呢。
这里附上一个自定义弹出框的小项目代码下载地址:点击打开链接
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Android自定义View设定到FrameLayout布局中实现多组件显示的方法 分享
-
Android实现在xml文件中引用自定义View的方法分析
-
C#实现在前端网页弹出警告对话框(alert)的方法
-
Android自定义控件实现万能的对话框
-
Android实现底部弹出的对话框功能
-
使用CSS3实现input多选框自定义样式的方法示例
-
Android ImageButton自定义按钮的按下效果的代码实现方法分享
-
Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方法
-
浅析Android Service中实现弹出对话框的坑
-
Android之用PopupWindow实现弹出菜单的方法详解