官方文档:
https://developer.android.com/reference/android/app/DialogFragment.html
public static MyDialogFragment newInstance(int num) {
MyDialogFragment f = new MyDialogFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments().getInt("num");
}
@Override
public void onStart() { //在onStart()中
super.onStart();
getDialog().getWindow().setBackgroundDrawableResource(R.drawable.background); //对话框背景
getDialog().getWindow().setLayout(300,200); //宽高
}
void showDialog() {
mStackLevel++;
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(ft, "dialog");
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //去除标题栏
return inflater.inflate(R.layout.dialog, container, false);
}
//点击外部不消失getDialog.setCanceledOnTouchOutside(false);
//点击返回键不消失,需要监听OnKeyListener:
getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
});
Window window = getDialog().getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.gravity = Gravity.BOTTOM; //底部
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(lp);
//设置对话框宽高也可以使用lp.width和lp.height
//利用接口传参,在DialogFragment中定义接口
public interface DialogListener {
void onComplete(String result);
}
//Activity实现该接口
public class MyActivity extends Activity implements DialogListener {
// ...
@Override
public void onComplete(String result){
//使用参数
}
}
//在DialogFragment中传参数
DialogListener listener=(DialogListener)getActivity();
listener.onComplete(result); //传参数
//在style.xml中引入自定义动画
//在Java代码中设置窗口动画
getDialog().getWindow().getAttributes().windowAnimations = R.style.CustomDialog;
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="300"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
set>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="0"
android:toYDelta="50%p"/>
<alpha
android:duration="300"
android:fromAlpha="1.0"
android:toAlpha="0.8"/>
set>
// 0~1 , 1表示完全昏暗
getDialog().getWindow().setDimAmount(0.8f);