DialogFragment使用时遇到的一些问题
来源:互联网 发布:局域网监控软件排行 编辑:程序博客网 时间:2024/05/16 12:43
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()中设置才有效.
@Overridepublic 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);
1 0
- DialogFragment使用时遇到的一些问题
- 使用DialogFragment遇到的一些问题
- 使用DialogFragment实现登录遇到的坑
- 使用thinkphp时遇到的一些问题
- dwz使用时遇到的一些问题
- 使用ObjectAnimator时遇到的一些问题
- 使用thymeleaf时遇到的一些问题
- 使用numpy时遇到的一些问题
- android中DialogFragment的一些使用笔记
- DialogFragment使用中show()方法遇到的IllegalStateException
- 使用desinger时遇到的一些问题的处理分享
- 使用VS2010时遇到的一些小问题的解决方案
- kotlin的一些学习和使用时遇到的问题
- 使用自动生成Mipmap时遇到的一些问题
- 使用GridView时遇到的一些小问题及解决方法
- 学习使用solr时遇到的一些问题,记录
- 使用wcscat_s函数时遇到的一些问题
- 使用SVN进行版本控制时遇到的一些问题
- 函数
- SICP_Python版:Chapter 2:Implement simple object system
- linux安装expressvpn以及使用指南
- java线程——模拟多窗口售票过程
- 八月英语——近朱者赤
- DialogFragment使用时遇到的一些问题
- 安装wamp时最后提示系统缺少msvcr110.dll的解决方法
- LoadLibrary python.plw error: 找不到指定的模块
- 算法入门
- 求字符串中字符只出现一次的的第一个字符
- 22.Flatten List-平面列表(容易题)
- UVA 208 Firetruck
- Java基础-Map集合之TreeMap
- 第三周《C语言及程序设计》实践项目38 用break和continue改变流程