您的位置:首页 > 移动开发 > Android开发

android中的Dialog

2016-04-22 15:58 821 查看

一、Dialog概述

二、使用系统自带的Dialog

1、新建Builder

AlertDialog.Builder builder = new AlertDialog.Builder(StoryActivity.this);
dialog.show();

2、通过builder创建dialog

AlertDialog dialog = builder.setView(view).setTitle("查看评论").setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();

3、关于NegativeButton、PositiveButton、NeutralButton

早期版本,NegativeButton最右侧、PositiveButton最左侧、NeutralButton中间



在android4.0中,NegativeButton在最左侧、PositiveButton在最右侧



二、如何自定义Dialog

1、特别注意!!!(首先明确)

1.1 setContentView、setView与show()方法相对位置引起异常

如使用setContentView、setView方法来自定义View的话,须与show()方法保持先后顺序

dialog.show();
dialog.setContentView(contentView);

2.2 自定义Dilog之后,setTitle等方法无效

须自行在界面中添加

2、步骤同上

3、设置界面

使用以下方法实现自定义界面

setView(View view)
setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight,int viewSpacingBottom)
setContentView(@LayoutRes int layoutResID)
setContentView(View view)
setContentView(View view, ViewGroup.LayoutParams params)//params Layout parameters for the view.

4、自定义Dialog的宽高

Window window = this.getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.height = dialogHeight;//设置你想要的宽高
window.setAttributes(params);

5、获得屏幕的宽高

DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高
d.heightPixels//取得px单位的值
d.xdpi//取得dp单位的值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: