郑州酒店网站建设,注册公司流程图,拉新充场app推广平台,杭州昨晚发生大事了前言#xff1a;圆角对话框在项目中用的越来越多#xff0c;之前一篇文章有介绍过使用系统的AlertDialogCardView(Android中使用CardView实现圆角对话框)实现了圆角对话框的样式#xff0c;今天介绍自定义Dialog实现通用的圆角对话框。效果图#xff1a;1.继承自AlertDialo…前言圆角对话框在项目中用的越来越多之前一篇文章有介绍过使用系统的AlertDialogCardView(Android中使用CardView实现圆角对话框)实现了圆角对话框的样式今天介绍自定义Dialog实现通用的圆角对话框。效果图1.继承自AlertDialog,重写onCreat/*** Created by ruancw on 2018/6/7.* 自定义的带圆角的对话框*/public class RoundCornerDialog extends AlertDialog{private TextView tvTitle;private TextView tvDes;private TextView tvCancel;private TextView tvConfirm;//private Context context;/*** 一个参数的构造方法* param context 上下文对象*/public RoundCornerDialog(NonNull Context context) {super(context);//this.contextcontext;}Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.dialog_layout_test);//设置背景透明不然会出现白色直角问题Window window getWindow();window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));setCanceledOnTouchOutside(false);//初始化布局控件initView();//确定和取消按钮的事件监听initEvent();//设置参数必须在show之后不然没有效果WindowManager.LayoutParams params getWindow().getAttributes();getWindow().setAttributes(params);}}注解决白色直角的问题(1)文中没有使用style设置背景透明直接在代码中用的window.setBackgroundDrawable设置的背景透明不然会出现遗留的四个角有白色直角的问题。(2)当然也可以在构造方法中这样设置super(context,R.style.CustomDialog)。2.初始化布局(1)布局文件(CradView实现圆角布局)xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_margindimen/dp_30app:cardCornerRadiusdimen/dp_10android:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:backgroundcolor/bg_mainWhiteandroid:orientationverticalandroid:idid/tv_titleandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:gravitycenterandroid:paddingdimen/dp_10android:text温馨提示android:textColorcolor/bg_mainWhiteandroid:textSizedimen/sp_18 /android:layout_widthmatch_parentandroid:layout_height1dpandroid:backgroundcolor/bg_line /android:idid/tv_desandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:paddingdimen/dp_20android:textSizedimen/sp_18 /android:layout_widthmatch_parentandroid:layout_height1dpandroid:backgroundcolor/bg_line /android:layout_widthmatch_parentandroid:layout_heightdimen/dp_48android:orientationhorizontalandroid:idid/tv_cancelandroid:layout_width0dpandroid:layout_heightmatch_parentandroid:layout_weight1.0android:gravitycenterandroid:text取消android:textSizedimen/sp_16 /android:layout_width1dpandroid:layout_heightmatch_parentandroid:backgroundcolor/bg_line /android:idid/tv_confirmandroid:layout_width0dpandroid:layout_heightmatch_parentandroid:layout_weight1.0android:gravitycenterandroid:text确定android:textSizedimen/sp_16 /(2)初始化布局文件及设置参数/*** 初始化布局文件及设置参数*/private void initView() {//对话框标题tvTitlefindViewById(R.id.tv_title);//对话框描述信息tvDesfindViewById(R.id.tv_des);//确定按钮和取消tvConfirmfindViewById(R.id.tv_confirm);tvCancelfindViewById(R.id.tv_cancel);}(3)设置事件监听让自定义的dialog实现OnClickListener接口然后设置确定及取消按钮的事件监听/*** 确定及取消点击事件*/private void initEvent() {tvConfirm.setOnClickListener(this);//确定tvCancel.setOnClickListener(this);//取消Overridepublic void onClick(View view) {switch (view.getId()){case R.id.tv_confirm:dismiss();break;case R.id.tv_cancel:dismiss();break;}}写到这里圆角对话框就实现了但如果另一个页面要求不同背景色按钮的文本也不是“确定”和“取消”呢我们是不是又的重写定义dialog和设置布局文件呢显然这样很麻烦貌似与我们的标题写的通用的圆角对话框也不相符啊这似乎不太好吧。接下来我们进行一番改造打造通用的圆角对话框。3.打造通用圆角对话框(1)initView中设置初始参数private String title温馨提示,message,confirmText确定,cancelText取消;//默认的标题栏背景色private int titleColorBgColor.parseColor(#FF8200);//默认的确定和取消按钮背景色private int confirmColorBgColor.parseColor(#F8F8F8);private int cancelColorBgColor.parseColor(#F8F8F8);/*** 初始化布局文件及设置参数*/private void initView() {//对话框标题tvTitlefindViewById(R.id.tv_title);//对话框描述信息tvDesfindViewById(R.id.tv_des);//确定按钮和取消tvConfirmfindViewById(R.id.tv_confirm);tvCancelfindViewById(R.id.tv_cancel);/********************通用设置*********************///设置标题、描述及确定按钮的文本内容tvTitle.setText(title);tvDes.setText(message);tvConfirm.setText(confirmText);//设置标题栏及确定、取消按钮背景色tvTitle.setBackgroundColor(titleColorBg);tvConfirm.setBackgroundColor(confirmColorBg);tvCancel.setBackgroundColor(cancelColorBg);}(2)定义设置属性方法/*** 设置标题栏文本* param title 标题*/public void setTitle(String title){this.titletitle;}/*** 设置描述信息* param message 描述信息*/public void setMessage(String message){this.messagemessage;}/*** 设置确定按钮上的文本* param confirmText 文本*/public void setConfirmText(String confirmText){this.confirmTextconfirmText;}/*** 设置取消按钮上的文本* param cancelText 文本*/public void setCancelText(String cancelText){this.cancelTextcancelText;}/*** 设置标题栏的背景* param titleColorBg 背景色int*/public void setTitleBg(int titleColorBg){this.titleColorBgtitleColorBg;}/*** 确定按钮背景色* param confirmColorBg int背景色*/public void setConfirmBg(int confirmColorBg){this.confirmColorBgconfirmColorBg;}/*** 取消按钮背景色* param cancelColorBg int背景色*/public void setCancelBg(int cancelColorBg){this.cancelColorBgcancelColorBg;}(3)定义接口实现确定按钮的点击回调private ConfirmListener confirmListener;/*** 设置确定按钮的监听* param confirmListener*/public void setConfirmListener(ConfirmListener confirmListener){this.confirmListenerconfirmListener;}/*** 确定按钮点击的监听接口*/public interface ConfirmListener{void onConfirmClick();}点击“确定”回调方法case R.id.tv_confirm:/************通用设置***********///点击确定按钮回调confirmListener.onConfirmClick();dismiss();break;一般点击“取消”按钮不做任何操作只是关闭当前弹出的对话框所以这里不做点击后回调当然点击“确定”后执行相关操作后也要关闭当前dialog。4.使用RoundCornerDialog roundCornerDialognew RoundCornerDialog(mContext);//设置标题描述文本等参数roundCornerDialog.setTitle(温馨提示);roundCornerDialog.setMessage(退出当前登录后将要重新登录);roundCornerDialog.setConfirmText(确认退出);//确定按钮的点击回调方法roundCornerDialog.setConfirmListener(new roundCornerDialog.ConfirmListener() {Overridepublic void onConfirmClick() {Intent intent new Intent(mContext, LoginActivity.class);startActivity(intent);UIUtil.toast(退出成功请重新登录);getActivity().finish();}});//显示对话框roundCornerDialog.show();总结本文通过自定义DialogCardView的方式实现了通用的圆角对话框效果使用也相对简单测试中发现在Android5.0以下设置标题栏背景色时标题栏不会跟随CardView的圆角。以上就是本文的全部内容希望对大家的学习有所帮助也希望大家多多支持脚本之家。