Android编程自定义AlertDialog样式的方法详解
2023年7月13日发(作者:)
Android编程⾃定义AlertDialog样式的⽅法详解本⽂实例讲述了Android编程⾃定义AlertDialog样式的⽅法。分享给⼤家供⼤家参考,具体如下:开发的时候,通常我们要⾃定义AlertDialog来满⾜我们的功能需求:⽐如弹出对话框中可以输⼊信息,或者要展⽰且有选择功能的列表,或者要实现特定的UI风格等。那么我们可以通过以下⽅式来实现。⽅法⼀:完全⾃定义AlertDialog的layout.如我们要实现有输⼊框的AlertDialog布局custom_: 原来在代码中使⽤:r builder = new r();View view = View .inflate(getActivity(), _dialog, null);w(view);celable(true);TextView title= (TextView) view .findViewById();//设置标题EditText input_edt= (EditText) view .findViewById(_edit);//输⼊内容Button btn_cancel=(Button)ewById(_cancel);//取消按钮 Button btn_comfirm=(Button)ewById(_comfirm);//确定按钮//取消或确定按钮监听事件处理AlertDialog dialog = ();();这样,我们就可以弹出⼀个我们⾃定义的Dialog。这种⽅式有个弊端就是:如果项⽬中有多个UI不同的AlertDialog,我们要写多个布局页⾯,当然可以提取通⽤布局,然后各种处理。⽅法2:通过修改 Android 系统原⽣的 AlertDialog 中的控件来达到我们想要的效果。⽐如我们要实现特定风格的对话框,我们可以写个公共的⽅法,通过修改 Android 系统原⽣的 AlertDialog 中的控件来达到我们想要的效果,简单代码如下:public static void setCustomDialogStyle(AlertDialog dialog){final Resources res = text().getResources(); int topPanelId = ntifier("topPanel", "id", "android");//获取顶部 LinearLayout topPanel = (LinearLayout) getDialog().findViewById(topPanelId); kgroundResource(_top_bg);//设置顶部背景 LayoutParams params = new LayoutParams(_PARENT, //设置顶部⾼度 dp2px(getDialog().getContext(), 50)); outParams(params); int dividerId = ntifier("titleDivider", "id", "android");//设置分隔线 View divider = getDialog().findViewById(dividerId); ibility(); int titleId = ntifier("alertTitle", "id", "android");//获取标题title TextView title = (TextView) getDialog().findViewById(titleId);//设置标题 tColor();//标题⽂字颜⾊ tSize(18);//⽂字⼤⼩ vity();//⽂字位置 int customPanelId = ntifier("customPanel", "id", "android");//设置内容 FrameLayout customPanel = (FrameLayout) getDialog().findViewById(customPanelId); kgroundColor(ARENT);//背景透明 ldAt(0).setBackgroundColor(); ding(dp2px(getContext(), 8), 0, 2px(getContext(), 8), 0);//设置padding int buttonPanelId = ntifier("buttonPanel", "id", "android");//获取底部 LinearLayout buttonPanel = (LinearLayout) getDialog().findViewById(buttonPanelId); kgroundResource(_bottom_bg);//设置底部背景 ding(dp2px(getContext(), 8), 1, dp2px(getContext(), 8), 0); Button button1 = (Button) getDialog().findViewById(1);//设置底部Button tColor();//⽂字颜⾊ tSize(18);//⽂字⼤⼩ kgroundResource(_right_round);//Button圆形背景框 Button button2 = (Button) getDialog().findViewById(2); tColor(); tSize(18); kgroundResource(_left_round);}代码中⽤到的各种颜⾊,背景图⽚等根据需求⾃⼰定义。⽤到的dp与px转换代码如下:public static int dp2px(Context context, float dp) { float density = ources().getDisplayMetrics().density; return (int) (dp * density + 0.5f);}这样我们就统⼀定义好了AlertDialog的整个界风格,在使⽤的时候,只需要根据UI需求定义内容部分的UI即可。还是上⾯可以输⼊的AlertDialog,我们的布局就可以只写成下⾯这个,当然,外⾯层的LinearLayout也是可以去掉的。 然后在代码中使⽤:r builder = new r();le("设置标题");View view = View .inflate(getActivity(), _dialog, null);w(view);celable(true);EditText input_edt= (QRCodeEditText) view .findViewById(_edt);itiveButton(, new kListener() { @Override public void onClick(DialogInterface dialog, int which) { //点击确定按钮处理 (); } } });ativeButton(, new kListener() { @Override public void onClick(DialogInterface dialog, int which) { //点击取消按钮处理 (); } });final AlertDialog dialog = ();();setCustomDialogStyle(dialog);//这⾥不要忘记调⽤setCustomDialogStyle⽅法这种⽅式 就⽐第⼀种⽅式⽅便 多了。当然要实现AlertDialog的背景透明等效果,我们还可以在res/value/内增加以下代码:在需要加⼊alertDialog的地⽅加⼊以下语句:r alertbBuilder=new r(getActivity(),);//接下来代码.....更多关于Android相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》及《》希望本⽂所述对⼤家Android程序设计有所帮助。
发布者:admin,转转请注明出处:http://www.yc00.com/web/1689217625a222482.html
评论列表(0条)