Android编程自定义AlertDialog样式的方法详解

本文实例讲述了Android编程自定义AlertDialog样式的方法。分享给大家供大家参考,具体如下:

开发的时候,通常我们要自定义AlertDialog来满足我们的功能需求:

比如弹出对话框中可以输入信息,或者要展示且有选择功能的列表,或者要实现特定的UI风格等。那么我们可以通过以下方式来实现。

方法一:完全自定义AlertDialog的layout.如我们要实现有输入框的AlertDialog布局custom_dialog.xml:



  
  
  
    

原来在代码中使用:

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View view = View
    .inflate(getActivity(), R.layout.custom_dialog, null);
builder.setView(view);
builder.setCancelable(true);
TextView title= (TextView) view
    .findViewById(R.id.title);//设置标题
EditText input_edt= (EditText) view
    .findViewById(R.id.dialog_edit);//输入内容
Button btn_cancel=(Button)view
.findViewById(R.id.btn_cancel);//取消按钮
 Button btn_comfirm=(Button)view
.findViewById(R.id.btn_comfirm);//确定按钮
//取消或确定按钮监听事件处理
AlertDialog dialog = builder.create();
dialog.show();

这样,我们就可以弹出一个我们自定义的Dialog。这种方式有个弊端就是:

如果项目中有多个UI不同的AlertDialog,我们要写多个布局页面,当然可以提取通用布局,然后各种处理。

方法2:通过修改 Android 系统原生的 AlertDialog 中的控件来达到我们想要的效果。

比如我们要实现特定风格的对话框,我们可以写个公共的方法,通过修改 Android 系统原生的 AlertDialog 中的控件来达到我们想要的效果,简单代码如下:

public static void setCustomDialogStyle(AlertDialog dialog){
final Resources res = dialog.getContext().getResources();
    int topPanelId = res.getIdentifier("topPanel", "id", "android");//获取顶部
    LinearLayout topPanel = (LinearLayout) getDialog().findViewById(topPanelId);
    topPanel.setBackgroundResource(R.drawable.dialog_top_bg);//设置顶部背景
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, //设置顶部高度
        dp2px(getDialog().getContext(), 50));
    topPanel.setLayoutParams(params);
    int dividerId = res.getIdentifier("titleDivider", "id", "android");//设置分隔线
    View divider = getDialog().findViewById(dividerId);
    divider.setVisibility(View.GONE);
    int titleId = res.getIdentifier("alertTitle", "id", "android");//获取标题title
    TextView title = (TextView) getDialog().findViewById(titleId);//设置标题
    title.setTextColor(Color.WHITE);//标题文字颜色
    title.setTextSize(18);//文字大小
    title.setGravity(Gravity.CENTER);//文字位置
    int customPanelId = res.getIdentifier("customPanel", "id", "android");//设置内容
    FrameLayout customPanel = (FrameLayout) getDialog().findViewById(customPanelId);
    customPanel.setBackgroundColor(Color.TRANSPARENT);//背景透明
    customPanel.getChildAt(0).setBackgroundColor(Color.WHITE);
    customPanel.setPadding(dp2px(getContext(), 8), 0, ViewUtils.dp2px(getContext(), 8), 0);//设置padding
    int buttonPanelId = res.getIdentifier("buttonPanel", "id", "android");//获取底部
    LinearLayout buttonPanel = (LinearLayout) getDialog().findViewById(buttonPanelId);
    buttonPanel.setBackgroundResource(R.drawable.dialog_bottom_bg);//设置底部背景
    buttonPanel.setPadding(dp2px(getContext(), 8), 1, dp2px(getContext(), 8), 0);
    Button button1 = (Button) getDialog().findViewById(android.R.id.button1);//设置底部Button
    button1.setTextColor(Color.WHITE);//文字颜色
    button1.setTextSize(18);//文字大小
    button1.setBackgroundResource(R.drawable.bg_right_round);//Button圆形背景框
    Button button2 = (Button) getDialog().findViewById(android.R.id.button2);
    button2.setTextColor(Color.WHITE);
    button2.setTextSize(18);
    button2.setBackgroundResource(R.drawable.bg_left_round);
}

代码中用到的各种颜色,背景图片等根据需求自己定义。用到的dp与px转换代码如下:

public static int dp2px(Context context, float dp) {
    float density = context.getResources().getDisplayMetrics().density;
    return (int) (dp * density + 0.5f);
}

这样我们就统一定义好了AlertDialog的整个界风格,在使用的时候,只需要根据UI需求定义内容部分的UI即可。

还是上面可以输入的AlertDialog,我们的布局就可以只写成下面这个,当然,外面层的LinearLayout也是可以去掉的。



  


然后在代码中使用:

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("设置标题");
View view = View
    .inflate(getActivity(), R.layout.custom_dialog, null);
builder.setView(view);
builder.setCancelable(true);
EditText input_edt= (QRCodeEditText) view
    .findViewById(R.id.input_edt);
builder.setPositiveButton(android.R.string.ok,
    new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        //点击确定按钮处理
          dialog.cancel();
        }
      }
    });
builder.setNegativeButton(android.R.string.cancel,
    new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
      //点击取消按钮处理
        dialog.cancel();
      }
    });
final AlertDialog dialog = builder.create();
dialog.show();
setCustomDialogStyle(dialog);//这里不要忘记调用setCustomDialogStyle方法

这种方式 就比第一种方式方便 多了。当然要实现AlertDialog的背景透明等效果,我们还可以在res/value/style.xml内增加以下代码:



在需要加入alertDialog的地方加入以下语句:

AlertDialog.Builder alertbBuilder=new AlertDialog.Builder(getActivity(),R.style.dialog);
//接下来代码.....

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

你可能感兴趣的:(Android编程自定义AlertDialog样式的方法详解)