在项目中,我们经常用到对话框,这里我直接介绍AlertDialog的基础类Dialog,因为在我们定制化对话框都是Dialog的。
(1) Dialog的创建
Dialog dlgShare = new Dialog(this);
dlgShare.show();
如果就如上仅仅new一个Dialog这样显示出的dialog是没有任何内容的。
(2) 设置Dialog的标题
dlgShare.setTitle("Test");
如果仅仅只设置了标题而没有设置内容那么对话框只显示个标题,如下:
(3) 设置对话框的内容
Dialog内容设置主要通过以下两个方法:
public void setContentView(int layoutResID)
public void setContentView(View view)
Eg:
Dialog dlgShare = new Dialog(this);
dlgShare.setContentView(R.layout.dialog_share);
dlgShare.setTitle("Test");
dialog_share.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="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#249EF6" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp" >
<TextView
android:layout_width=