DialogFragment使用总结
来源:互联网 发布:空间句法分析软件知乎 编辑:程序博客网 时间:2024/05/29 03:20
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/,报我名字给优惠
0 0
- DialogFragment使用总结
- DialogFragment使用总结
- DialogFragment使用及总结
- Android 必知必会 - DialogFragment 使用总结
- 使用DialogFragment
- dialogfragment使用
- DialogFragment 使用
- DialogFragment 使用
- 使用DialogFragment
- DialogFragment使用
- 使用DialogFragment
- DialogFragment使用
- DialogFragment总结与封装
- Using DialogFragment ——使用DialogFragment
- Android:DialogFragment的使用
- Dialog与DialogFragment使用
- ApiDemos--DialogFragment的使用
- android DialogFragment简单使用
- 数据结构-Java实现队列
- C#十七章项目
- poj3094
- 【基于WPF+OneNote+Oracle的中文图片识别系统阶段总结】之篇二:基于OneNote难点突破和批量识别
- Unity飞机大战 源码 分享 学习
- DialogFragment使用总结
- 关于make提示must be enabled with the -std=c++11 or -std=gnu++11 compiler options
- 总结
- 线性代数学习笔记5
- where方法的应用
- 深度学习与计算机视觉系列(3)_线性SVM与SoftMax分类器
- Hive的几种数据导入方式
- storm消费kafka数据
- pow(x,n)的实现