自定义AlertDialog样式,根据屏幕大小来显示

介绍一些关于AlertDialog的基本知识:

    一、AlertDialog简介:AlertDialog的构造方法被声明为protected,所以不能直接使用new关键字来创建 AlertDialog类的对象实例。要想创建AlertDialog对话框,需要使用Builder类,该类是AlertDialog类中定义的一个内 嵌类。因此必须创建AlertDialog.Builder类的对象实例,然后再调用show()来显示对话框。


    二、使用AlertDialog创建对话框的种类:


        1. 最多带3个按钮的对话框:setPositiveButton(...)--确认、setNegativeButton(...)--取消、setNeutralButton(...)--忽略


        2.简单列表对话框:通过AlertDialog.Builder类的setItems(...)方法可以创建简单的列表对话框。其实,这种类型的对话框相当于将ListView组件放在对话框上,然后再在ListView中添加若干简单的文本。


        3.单选列表对话框:通过AlertDialog.Builder类的setSingleChoiceItems(...)来创建。目前支持4种数据源(数组资源、数据集、字符串数组、ListAdapter)


        4.多选列表对话框:通过AlertDialog.Builder类的setMultiChoiceItems(...)创建。目前支持3种数据源(数组资源、数据集、字符串数组)


        5.水平进度或圆形对话框(默认是:圆形):该类型的对话框是通过ProgressDialog来实现,该类是AlertDialog的子类,它不需要用create()方法来返回实例对象,只需要new即可。


           ProgressDialog.STYLE_HORIZONTAL //水平进度样式


           ProgressDialog.STYLE_SPINNER    //圆形样式


        6.自定义对话框:直接使用XML布局文件或以编写JAVA代码方式来创建视图,并将这些视图对象添加到对话框中去。


        7.使用Activity托管对话框:Activity类中也提供了创建对话框的方式,有个onCreateDialog(int id)的方法,其返回类型是Dialog,通过是当调用Activity类的showDialog(int id)方法时,系统会调用该方法来返回一个Dialog对象。showDialog和onCreateDialog都有一个int类型的id参数,该参数 值将传递给onCreateDialog方法。因此,我们可以利用不同的id创建多个对话框。


         ***注意***:对于表示某一个对话框的ID,系统只在第1次调用showDialog方法时调用onCreateDialog方法。在第1次创建 Dialog对象时系统会将该对象保存在Activity的缓存里,相当于一个Map对象,对话框的ID作为Map的Key,而Dialog对象作为 Map的Value。下次再调用时,会先根据这个ID从Map中获得第1次创建的Dialog对象。除非该ID已经被删除。


        8.悬浮对话框和触摸任何位置都可以关闭的对话框:


        1).悬浮对话框:android:theme="@android:style/Theme.Dialog";对于该类型的对话框,触摸屏幕任何位置都会触发Activity的OnTouchEvent事件。


         2).触摸任何位置都可以关闭的对话框:首先必须要继承AlertDialog类,并重写OnTouchEvent事件。


第一种:


Java代码

  1. /**
  2. * 自定义AlertDialog
  3. *
  4. * @author chenjianli 2011-05-10
  5. */
  6. public void alert(){
  7.         WindowManager manager = getWindowManager();
  8.         Display display = manager.getDefaultDisplay();
  9.         int width = display.getWidth();
  10.         int height = display.getHeight();

  11.         LayoutInflater inflater = getLayoutInflater();
  12.         View view = inflater.inflate(R.layout.alert, null);

  13.         TextView text = (TextView)view.findViewById(R.id.text);
  14.         text.setText("自定义AlertDialog");

  15.         AlertDialog alert = new AlertDialog.Builder(this).create();
  16.         alert.show();

  17.         alert.getWindow().setLayout(width/2, height/4);
  18.         alert.setTitle("测试");
  19.         alert.getWindow().setContentView(R.layout.alert);
  20. }
复制代码

第二种:
Java代码

  1. /**
  2. * 自定义AlertDialog
  3. *
  4. * @author chenjianli 2011-05-10
  5. */

  6. AlertDialog zidongbofangDialog = new AlertDialog.Builder(ManHuaActivity.this).create();
  7. zidongbofangDialog.show();
  8. zidongbofangDialog.getWindow().setGravity(Gravity.CENTER);
  9. zidongbofangDialog.getWindow().setLayout(
  10. android.view.WindowManager.LayoutParams.FILL_PARENT,
  11. android.view.WindowManager.LayoutParams.WRAP_CONTENT);
  12. zidongbofangDialog.getWindow().setContentView(R.layout.manhua_dialog_zidongbofang);
复制代码

第三种:
/**
* 自定义AlertDialog
*
* @author chenjianli 2011-05-10
*/
如果我们setView(),中的View是带EditText的,此时,我们必须在show()之前加上这么一句话,才可以在点击EditText时弹出键盘,否则将很杯具!键盘是弹不出来的。
Java代码

  1. AlertDialog tiaozhuanDialog= new AlertDialog.Builder(ManHuaActivity.this).create();
  2. tiaozhuanDialog.setView(getLayoutInflater().inflate(R.layout.manhua_dialog_tiaozhuan, null));
  3. tiaozhuanDialog.show();
  4. tiaozhuanDialog.getWindow().setGravity(Gravity.CENTER);
  5. tiaozhuanDialog.getWindow().setLayout(
  6. android.view.WindowManager.LayoutParams.FILL_PARENT,
  7. android.view.WindowManager.LayoutParams.WRAP_CONTENT);
  8. tiaozhuanDialog.getWindow().setContentView(getLayoutInflater().inflate(R.layout.manhua_dialog_tiaozhuan, null));
复制代码

这里还有一个地方需要注意一下,如果我们在show这个AlertDialog之前,需要设置该AlertDialog显示的View中的EditText的内容,则我们应该这么去findViewById():
Java代码

  1. EditText editText = (EditText)tiaozhuanDialog.findViewById(R.id.myEditText);
  2. editText.setText("Who are you ? I am android Developer ");
复制代码

否 则会报ERROR/AndroidRuntime(1032): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.错误!!

 1 1)更改AlertDialog窗口大小的方法:
2 AlertDialog dialog = new AlertDialog.Builder(this).create();
3 dialog.show();
4 WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
5 params.width = 200;
6 params.height = 200 ;
7 dialog.getWindow().setAttributes(params);
8
9 2)去除边框
10 AlertDialog.setView(view,0,0,0,0);


  1. AlertDialog dialog = builder.setTitle("消息列表")  
  2.                     .setView(layout)  
  3.                     .create();  
  4. dialog.show();  
  5. //设置窗口的大小  
  6. dialog.getWindow().setLayout(300200);  

dialog.show();一定要放在dialog.getWindow().setLayout(300, 200);的前面,否则不起作用。

网上有一种方法是
  1. WindowManager.LayoutParams params = dialog.getWindow().getAttributes();  
  2. params.width = 300;  
  3. params.height = 200;  
  4. dialog.getWindow().setAttributes(params);  
但是dialog.getWindow().setLayout(300, 200);实际上封装了这个方法,setLayout()的源代码如下:
  1. final WindowManager.LayoutParams attrs = getAttributes();  
  2. attrs.width = width;  
  3. attrs.height = height;  
  4. if (mCallback != null) {  
  5.     mCallback.onWindowAttributesChanged(attrs);  
  6. }  

所以这两个方法的作用本质上是一样的,都是为AlertDialog设置大小
原文地址:https://www.cnblogs.com/jiezzy/p/2694917.html