着重说明(官方):☆☆☆☆☆
1.Dialog是对话框的基类,最好不要直接实例化Dialog,而应使用它的子类AlertDialog
2.最好的写法就是将DialogFragment作为容器,搭配AlertDialog使用(因为这样能更好的处理生命周期)
3.如果你想自定义对话框,建议不要使用Dialog,而直接使用Activity的对话框
(注意:一定要记牢上面三句话,这样你才能灵活自如的使用Dialog)
(注意:一定要记牢上面三句话,这样你才能灵活自如的使用Dialog)
(注意:一定要记牢上面三句话,这样你才能灵活自如的使用Dialog)
这些代码写起来都还算简单,选几个常用的作为案例:
案例一、带有点击回调的案例(上图中第一个)
public class NormalDialogFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("message")
.setPositiveButton("sure", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mListener.onDialogPositiveClick(NormalDialogFragment.this);
}
}).setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mListener.onDialogNegativeClick(NormalDialogFragment.this);
}
});
return builder.create();
}
/* The activity that creates an instance of this dialog fragment must
* implement this interface in order to receive event callbacks.
* Each method passes the DialogFragment in case the host needs to query it. */
public interface NormalDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
// Use this instance of the interface to deliver action events
NormalDialogListener mListener;
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
// Instantiate the NormalDialogListener so we can send events to the host
mListener = (NormalDialogListener) context;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(context.toString()
+ " must implement NormalDialogListener");
}
}
}
public void showDialog(View view) {
NormalDialogFragment customDialog = new NormalDialogFragment();
customDialog.show(getSupportFragmentManager(), "NormalDialogFragment");
}
案例二、Activity完全作为Dialog显示(上图第7个,和普通activity只是设置不同的主题)
<style name="activityDialogStyle" parent="Theme.AppCompat.Light.NoActionBar">
<!--设置dialog的背景-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--设置Dialog的windowFrame框为无-->
<item name="android:windowFrame">@null</item>
<!--设置无标题-->
<item name="android:windowNoTitle">true</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否半透明-->
<item name="android:windowIsTranslucent">true</item>
<!--设置窗口内容不覆盖-->
<item name="android:windowContentOverlay">@null</item>
<!--设置动画,在这里使用让它继承系统的Animation.Dialog-->
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<!--背景是否模糊显示-->
<item name="android:backgroundDimEnabled">true</item>
</style>
案例三、显示任意位置的Activity主题设置:(原理:就是通过计算出当前dialogActivity在父Activity的局对位置,然后通过Margin属性去设置)
<style name="MyDialogLocation" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
</style>
源码链接:http://download.youkuaiyun.com/download/android_xue/10175370
官方链接:https://developer.android.google.cn/guide/topics/ui/dialogs.html#FullscreenDialog