1.系统默认对话框设置
这是调用系统默认对话框,这里说一下creat和show的区别,在Android的API中说如果你要在显示这个dialog之前有一些操作就调用creat,如果没有就调用show
当然,我们可以先调用creat再调用show也是可以的。
关于警示对话框的自定义:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/dialog"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="48.0dip"
android:layout_marginLeft="10.0dip"
android:layout_marginRight="10.0dip"
android:background="@color/white" >
<TextView
android:id="@+id/contentmsg"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:text="TextView"
android:textColor="@color/black_text"
android:textSize="@dimen/Text_size_m" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:layout_alignParentBottom="true"
android:background="@color/green_bg" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="@color/white"
android:gravity="bottom|center"
android:orientation="horizontal"
android:layout_marginLeft="10.0dip"
android:layout_marginRight="10.0dip" >
<RelativeLayout
android:id="@+id/dialogokbtn"
android:layout_width="wrap_content"
android:layout_height="48.0dip"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:text="确定"
android:textColor="@color/green_text"
android:textSize="@dimen/Text_size_m" />
</RelativeLayout>
<View
android:layout_width="1px"
android:layout_height="80.0dip"
android:background="@color/green_bg" />
<RelativeLayout
android:id="@+id/dialogcancelbtn"
android:layout_width="wrap_content"
android:layout_height="48.0dip"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:text="取消"
android:textColor="@color/green_text"
android:textSize="@dimen/Text_size_m" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
自定义的Layout布局如下图:
第一种自定义对话框实现:
setView(layout)一定要在layout.findViewById(...).setText(...)之后,要不然builder加载的对话框不会把setText的内容加载进去。还有一个就是在dialogcancelbtn的监听事件里,不能调用builder的dimiss方法退出对话框,因为此时对话框还没有被加载,怎么退出对话框目前还在研究中,希望知道的朋友告诉我一声,不胜感激。
第二种自定义对话框实现:
此方法先把对话框show出来,然后再往上面填充layout。create和show一定要放前面,要不会报异常。
第三种自定义对话框:
这种自定义对话框其实和第一种差不多,只不过这里的确定和取消按钮调用了builder本身的函数。
在这三种自定义的对话框中都出现了LayoutInflater,简要介绍一下:
LayoutInflater主要用于自定义的布局,和findViewById差不多,区别在于前者获取Layout的Id,或者获取xml上控件的Id。
LayoutInflater的三种创建方法:
第一种:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.dialoglayout, null);
第二种:
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.dialoglayout, null);
第三种:
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null);
个人感觉差不多,习惯用哪种就用哪种。