DialogFragment是一个提示框的类,今天写一下平时用到的自定义Dialog的方法:
- 设置Dialog的显示宽度与位置
@Override
public void onResume() {
Window window = getDialog().getWindow();
Point size = new Point();
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
//set width=screenwidth*0.875 height=wrap_content
window.setLayout((int) (size.x * 0.875), WindowManager.LayoutParams.WRAP_CONTENT);
//set dialog location center
window.setGravity(Gravity.CENTER);
// Call super onResume after sizing
super.onResume();
}
- 去除掉DialogFragment默认的标题栏
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
- 添加动画效果(Dialog的动画效果)
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getDialog().getWindow().getAttributes().windowAnimations= R.style.CollectDialogAnimation;
}
<style name="CollectDialogAnimation">
//Dialog show的效果 slide_up可以自己定义(我都是百度的)
<item name="android:windowEnterAnimation">@anim/slide_up</item>
//Dialog dismiss的效果
<item name="android:windowExitAnimation">@anim/slide_down</item>
</style>
- 设置Dialog是否可以被取消
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
//设置为不可取消
dialog.setCancelable(false);
//设置为dialog外点击不可取消
dialog.setCanceledOnTouchOutside(false);
//设置点击返回键或搜索键不可取消
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_SEARCH||keyCode==KeyEvent.KEYCODE_BACK)
{
return true;
}
else
{
return false; //默认返回 false
}
}
});
return dialog;
}
- 设置自定义layout
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_layout, container, false);
}
- Layout事件的响应
//buttonClickListener是一个接口,初始化Dialog的时候可以设置,这样Dialog里面的点击就可以传给父界面处理
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (view != null) {
//a=view.findViewById(R.id.a);
findViews();
if (getArguments() != null)
btnA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (buttonClickListener != null) {
buttonClickListener.onLeftButtonClick();
dismiss();
}
}
});
btnB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (buttonClickListener != null) {
buttonClickListener.onRightButtonClick();
dismiss();
}
}
});
}
}
}
- 设置DialogFragment为圆角对话框
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().getWindow().setBackgroundDrawableResource(R.drawable.dialog_circle);
return inflater.inflate(R.layout.dialog_validate_code, container, false);
}
//DialogFragment
//dialog_circle
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/light_white" />
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"></corners>
</shape>
注:R.layout.dialog_validate_code中根Layout必须宽度必须是wrap_content,而且background属性不能设置
都没有写DialogFragment的基本用法,如果大家需要的话,请留言,谢谢。一个行走中的菜鸟,推荐大神带你飞系列:http://stay4it.com/,报我名字给优惠