开发中android自带dialog样式有时候不能满足我们的需求,这时候就需要自定义样式了,调用dialog的setView方法可以自定义布局,代码如下

final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

        View alertDialogView = View.inflate(context, R.layout.comment_input_layout, null);
        final AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.setView(alertDialogView, 0, 0, 0, 0);

        final EditText editText = (EditText) alertDialogView.findViewById(R.id.etContent);

        alertDialog.show();

//      设置dialog从底部出现动画
        Window window = alertDialog.getWindow();
        window.setContentView(R.layout.comment_input_layout);
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.comment_input_dilog_anim_style);
        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = alertDialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth()); //设置宽度
        window.setAttributes(lp);
        });
View Code

相关文章:

  • 2021-12-30
  • 2022-01-01
  • 2021-11-04
  • 2020-10-25
  • 2021-09-02
  • 2021-08-28
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-18
  • 2022-02-09
  • 2021-08-17
  • 2021-10-10
  • 2022-12-23
  • 2021-10-11
  • 2021-06-19
相关资源
相似解决方案