DialogFragment使用时遇到的一些问题
2016-08-27 20:34
411 查看
DialogFragment使用时遇到的一些问题
官方文档:https://developer.android.com/reference/android/app/DialogFragment.html
Activity向Dialog传递参数
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"); }
设置宽高和对话框背景等
在xml中设置和onCreateView(), onViewCreated()中设置无效. 在onStart()和onResume()中设置才有效.@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; } });
设置Dialog位于屏幕底部
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
Dialog向Activity传参
//利用接口传参,在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); //传参数
Enter和Exit动画 (飞入飞出)
//在style.xml中引入自定义动画 <style name="CustomDialog" parent="@android:style/Theme.Dialog"> <item name="android:windowEnterAnimation">@anim/popwin_show_anim</item> <item name="android:windowExitAnimation">@anim/popwin_hide_anim</item> </style> //在Java代码中设置窗口动画 getDialog().getWindow().getAttributes().windowAnimations = R.style.CustomDialog;
popwin_show_anim.xml代码
<?xml version="1.0" encoding="utf-8"?> <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>
popwin_hide_anim.xml代码
<?xml version="1.0" encoding="utf-8"?> <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>
设置背景Activity的明暗度
// 0~1 , 1表示完全昏暗 getDialog().getWindow().setDimAmount(0.8f);
相关文章推荐
- VC 使用过程中所遇到的一些问题,汇总在这里,希望对后来者有些许帮助
- 使用webchart过程中遇到的一些问题
- 使用Linq 更新数据库时遇到的一些问题及解决办法
- Struts2.1.8 验证框架(validation)的使用中遇到的一些问题(直接访问jsp遇到的错误)--Allen
- log4net使用中遇到的一些问题
- .net在使用Lucene中遇到的一些问题总结
- Java赋值运算符(+=、-=、*=、/=)使用中遇到的一些问题
- 使用S3C2440开发板遇到的一些问题
- 关于IIS在使用中遇到的一些问题的总结
- 使用ajax遇到的一些小问题
- 在使用ArcGIS过程中遇到的一些问题
- 在使用Struts的过程中也遇到了一些问题,现在总结一下
- [项目过程中所遇到的各种问题记录]ORM篇——使用NHibernate配置对象实体的一些小问题
- SQLSERVER2000使用中遇到的一些小问题,不断更新中。。。
- 关于用c生成的dll在使用其他供应商的工具创建可执行模块时遇到的一些问题
- 使用自动生成Mipmap时遇到的一些问题
- 使用 swfupload 遇到的一些问题
- Struts2.1.8 验证框架(validation)的使用中遇到的一些问题(直接访问jsp遇到的错误)
- [项目过程中所遇到的各种问题记录]ORM篇——使用NHibernate配置对象实体的一些小问题
- 使用LWUIT中遇到的一些问题