<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:14px;"> DialogFragment是基于Fragment展示Dialog的窗口。对话框的控制(何时隐藏、显示、关闭)通过对应的Dialogment 的API来完成,而不是Dialog的API.</span></span>
DialogFragment需要保证Fragment和Dialog的状态保持一致。它监听dialog的dissmiss event,并且去处理自己的状态。这意味着应该调用 show(FragmentManager, String)或者show(FragmentTransaction, String)来添加一个DialogFragment的实例给UI层,并且它自主将remove当dialog消失的时候。
实现这个类创建Dialog,需要通过重写法onCreateView(LayoutInflater, ViewGroup, Bundle)来填充dialog的内容,或者重写方法onCreateDialog(Bundle)来创建一个完全自定义的dialog。
1. 重写onCreateView创建Dialog
(a)布局文件,一个登陆窗口的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="18sp"/>
<EditText
android:id="@+id/edit_logiName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:hint="请输入名字"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="18sp" />
<EditText
android:id="@+id/edit_logiPwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:hint="请输入密码"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorPrimary"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00000000"
android:text="取消"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
/>
<Button
android:id="@+id/btn_login"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#00000000"
android:layout_weight="1"
android:text="登录"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorAccent"
/>
</LinearLayout>
(b) 继承DialogFragmeng重写onCreateView方法
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dia_login, container, false);
return view;
}
(c) 测试展示
LoginDialog loginDialog = LoginDialog.newInstance();
loginDialog.show(getFragmentManager(), "dilog");
(d) 设置Dialog样式
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments().getInt("num");
int style = DialogFragment.STYLE_NORMAL, theme = 0;
switch (mNum) {
case 1: style = DialogFragment.STYLE_NO_TITLE; break;
case 2: style = DialogFragment.STYLE_NO_FRAME; break;
case 3: style = DialogFragment.STYLE_NO_INPUT; break;
case 4: style = DialogFragment.STYLE_NORMAL; break;
case 5: style = DialogFragment.STYLE_NORMAL; break;
case 6: style = DialogFragment.STYLE_NO_TITLE; break;
case 7: style = DialogFragment.STYLE_NO_FRAME; break;
case 8: style = DialogFragment.STYLE_NORMAL; break;
}
switch (mNum) {
case 4: theme = android.R.style.Theme_Holo; break;
case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break;
case 6: theme = android.R.style.Theme_Holo_Light; break;
case 7: theme = android.R.style.Theme_Holo_Light_Panel; break;
case 8: theme = android.R.style.Theme_Holo_Light;
break;
}
2.重写onCreateDialog方法
(a) DIalog布局同上
(b) 继承DialogFragment,重写OnCreateDialog,创建Dialog
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(R.layout.dia_login, null, false);
initUI(view);
setListener();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view);
return builder.create();
(c) 在合适的位置展示Dialog(d)设置Dialog样式
在XML中定义
<style name="DialogStyle" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@drawable/settings_window</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:background">@android:color/background_light</item>
<item name="android:windowFullscreen">true</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
创建Dialog时调用相应的stylepublic Dialog onCreateDialog(Bundle savedInstanceState) {
*******
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.DialogStyle);
******
}
集思广益、坚持不懈的总结,总会进步!
参考资料:
1.http://www.android-doc.com/reference/android/app/DialogFragment.html
2.https://github.com/codepath/android_guides/wiki/Using-DialogFragment
3.http://blog.youkuaiyun.com/pipisky2006/article/details/8547539