先看效果图:
1.新建一个类,并extends Dialog。
public class MyDialog extends Dialog implements android.view.View.OnClickListener{
private Context context;
private String hintTitle,hintDescription;//dialog对话框文字内容
private TextView tv_ok,tv_cancel,tv_hint,tv_hintDescription;
public MyDialog(Context context,String hintTitle,String hintDescription) {
super(context,R.style.MyDialog);
// TODO Auto-generated constructor stub
this.hintTitle=hintTitle;//从activity传递过来
this.hintDescription=hintDescription;//从activity传递过来
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_hint);//对话框布局
initViews();
}
private void initViews(){
tv_ok=(TextView)findViewById(R.id.ok);
tv_ok.setOnClickListener(this);
tv_cancel=(TextView)findViewById(R.id.cancel);
tv_cancel.setOnClickListener(this);
tv_hint=(TextView)findViewById(R.id.hint);
tv_hintDescription=(TextView)findViewById(R.id.hint_description);
tv_hint.setText(hintTitle);
tv_hintDescription.setText(hintDescription);
setCanceledOnTouchOutside(false);//点击外部对话框不消失
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.ok:
dismiss();
break;
case R.id.cancel:
dismiss();
break;
default:
break;
}
}
}
2.写dialog布局dialog_hint.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/round_white_shape"//为了美观这里写了圆角背景
android:orientation="vertical" >
<TextView
android:id="@+id/hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:padding="6dp"
android:shadowRadius="1.5"
android:text="错误提示"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/hint_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:padding="6dp"
android:text="用户名或密码不正确,请重新再试"
android:textColor="#666666"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#DFDFDF" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:gravity="center"
android:text="确定"
android:textColor="#0dbfc6"
android:textSize="20sp" />
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:background="#DFDFDF" />
<TextView
android:id="@+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:gravity="center"
android:text="取消"
android:textColor="#0dbfc6"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
3.将dialog实例化
public class MainActivity extends Activity implements OnClickListener{
private TextView tv_dialog;
private MyDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_dialog=(TextView)findViewById(R.id.tv);
tv_dialog.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.tv:
dialog=new MyDialog(this, "猜我是谁", "我就是自定义对话框");
dialog.show();
setDialogWindow(dialog);//设置对话框大小和位置
break;
default:
break;
}
}
public void setDialogWindow(Dialog dialog) {
Window window = dialog.getWindow();
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = width / 5 * 4;
lp.height = height / 5 * 2;
window.setAttributes(lp);
}
}
PS:自定义对话框的布局可以是各种,比如列表,进度条等等,根据需求改变。
demo下载地址:
http://download.csdn.net/detail/kinglong68/9471716