介绍一些关于AlertDialog的基本知识:
Java代码
- /**
- * 自定义AlertDialog
- *
- * @author chenjianli 2011-05-10
- */
- public void alert(){
- WindowManager manager = getWindowManager();
- Display display = manager.getDefaultDisplay();
- int width = display.getWidth();
- int height = display.getHeight();
- LayoutInflater inflater = getLayoutInflater();
- View view = inflater.inflate(R.layout.alert, null);
- TextView text = (TextView)view.findViewById(R.id.text);
- text.setText("自定义AlertDialog");
- AlertDialog alert = new AlertDialog.Builder(this).create();
- alert.show();
- alert.getWindow().setLayout(width/2, height/4);
- alert.setTitle("测试");
- alert.getWindow().setContentView(R.layout.alert);
- }
第二种:
Java代码
- /**
- * 自定义AlertDialog
- *
- * @author chenjianli 2011-05-10
- */
- AlertDialog zidongbofangDialog = new AlertDialog.Builder(ManHuaActivity.this).create();
- zidongbofangDialog.show();
- zidongbofangDialog.getWindow().setGravity(Gravity.CENTER);
- zidongbofangDialog.getWindow().setLayout(
- android.view.WindowManager.LayoutParams.FILL_PARENT,
- android.view.WindowManager.LayoutParams.WRAP_CONTENT);
- zidongbofangDialog.getWindow().setContentView(R.layout.manhua_dialog_zidongbofang);
第三种:
/**
* 自定义AlertDialog
*
* @author chenjianli 2011-05-10
*/
如果我们setView(),中的View是带EditText的,此时,我们必须在show()之前加上这么一句话,才可以在点击EditText时弹出键盘,否则将很杯具!键盘是弹不出来的。
Java代码
- AlertDialog tiaozhuanDialog= new AlertDialog.Builder(ManHuaActivity.this).create();
- tiaozhuanDialog.setView(getLayoutInflater().inflate(R.layout.manhua_dialog_tiaozhuan, null));
- tiaozhuanDialog.show();
- tiaozhuanDialog.getWindow().setGravity(Gravity.CENTER);
- tiaozhuanDialog.getWindow().setLayout(
- android.view.WindowManager.LayoutParams.FILL_PARENT,
- android.view.WindowManager.LayoutParams.WRAP_CONTENT);
- tiaozhuanDialog.getWindow().setContentView(getLayoutInflater().inflate(R.layout.manhua_dialog_tiaozhuan, null));
这里还有一个地方需要注意一下,如果我们在show这个AlertDialog之前,需要设置该AlertDialog显示的View中的EditText的内容,则我们应该这么去findViewById():
Java代码
- EditText editText = (EditText)tiaozhuanDialog.findViewById(R.id.myEditText);
- 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);
- AlertDialog dialog = builder.setTitle("消息列表")
- .setView(layout)
- .create();
- dialog.show();
- //设置窗口的大小
- dialog.getWindow().setLayout(300, 200);
dialog.show();一定要放在dialog.getWindow().setLayout(300, 200);的前面,否则不起作用。
网上有一种方法是
- WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
- params.width = 300;
- params.height = 200;
- dialog.getWindow().setAttributes(params);
但是dialog.getWindow().setLayout(300, 200);实际上封装了这个方法,setLayout()的源代码如下:
- final WindowManager.LayoutParams attrs = getAttributes();
- attrs.width = width;
- attrs.height = height;
- if (mCallback != null) {
- mCallback.onWindowAttributesChanged(attrs);
- }
所以这两个方法的作用本质上是一样的,都是为AlertDialog设置大小