DialogFragment的简单使用
来源:互联网 发布:mac qq 讨论组 删除人 编辑:程序博客网 时间:2024/05/22 11:37
先看看效果图
1、 概述
DialogFragment在android 3.0时被引入。是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框。典型的用于:展示警告框,输入框,确认框等等。
在DialogFragment产生之前,我们创建对话框:一般采用AlertDialog和Dialog。注:官方不推荐直接使用Dialog创建对话框。
2、 好处与用法
使用DialogFragment来管理对话框,当旋转屏幕和按下后退键时可以更好的管理其声明周期,它和Fragment有着基本一致的声明周期。且DialogFragment也允许开发者把Dialog作为内嵌的组件进行重用,类似Fragment(可以在大屏幕和小屏幕显示出不同的效果)。上面会通过例子展示这些好处~
使用DialogFragment至少需要实现onCreateView或者onCreateDIalog方法。onCreateView即使用定义的xml布局文件展示Dialog。onCreateDialog即利用AlertDialog或者Dialog创建出Dialog。
3,重写onCreateView创建Dialog的布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/gray_f2" android:gravity="center" android:paddingBottom="@dimen/dp_10" android:paddingTop="10dp" android:text="@string/add_car" android:textSize="@dimen/text_size_16"/> <View android:layout_width="match_parent" android:layout_height="1px" android:layout_marginLeft="@dimen/dp_15" android:layout_marginRight="@dimen/dp_15" android:background="@color/gray_69"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/gray_f2" android:orientation="horizontal" android:paddingBottom="10dp" android:paddingTop="@dimen/dp_10"> <LinearLayout android:id="@+id/dialog_bottom_one" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center_horizontal" android:background="@mipmap/mycar"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:text="@string/car_lib_choose" android:textColor="@color/gray_69" android:textSize="@dimen/text_size_14"/> </LinearLayout> <View android:layout_width="1px" android:layout_height="match_parent" android:background="@color/gray_69"/> <LinearLayout android:id="@+id/dialog_bottom_two" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center_horizontal" android:background="@mipmap/byhand"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:text="@string/edit_choose_car" android:textColor="@color/gray_69" android:textSize="@dimen/text_size_14"/> </LinearLayout> </LinearLayout> <Button android:id="@+id/dialog_bottom_btnCancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:padding="5dp" android:text="@string/cancel" android:textColor="@color/black_4a" android:textSize="@dimen/text_size_20"/></LinearLayout>
4,自定义dialogFragment
public class MyDialog extends DialogFragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //隐藏title getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //设置dialog的 进出 动画 getDialog().getWindow().setWindowAnimations(R.style.pop_bottom_anim); // 设置在屏幕的底部。 Window window = getDialog().getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); lp.gravity = Gravity.BOTTOM; window.setBackgroundDrawable(new ColorDrawable(0xff000000)); window.setAttributes(lp); //设置rootView View rootView = inflater.inflate(R.layout.dialog_bottom, null); return rootView; } }
5,设置dialog的style
<style name="pop_bottom_anim" parent="android:Animation"> <item name="android:windowEnterAnimation">@anim/dialog_bottom_in</item> <item name="android:windowExitAnimation">@anim/dialog_bottom_out</item> </style>
dialogFragment进入时的动画
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="200" android:fromYDelta="100%p" android:toYDelta="0" /> <alpha android:duration="200" android:fromAlpha="0.0" android:toAlpha="1.0" /></set>
dialogFragment退出时的动画
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="200" android:fromYDelta="0" android:toYDelta="50%p" /> <alpha android:duration="200" android:fromAlpha="1.0" android:toAlpha="0.0" /></set>
如何调用呢,只需要一行代码
MyDialog myDialog = new MyDialog(); myDialog.show(getActivity().getFragmentManager(), "myDialog");
是乎就已经完事了,但是你会发现弹出的dialog是这样的。
那么问题来了,是什么导致两边还有边距的呢,我也很纳闷。。。
我记得popupwindow显示不全的时候需要获取屏幕的宽高,并且设置给popupwindow
DisplayMetrics dm = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(dm);popupwindow mResendMailPopup = newPopupWindow(mResendMailPopupView,(int) (dm.widthPixels), (int) (dm.heightPixels), true); //没有设置宽高显示不全的问题 mResendMailPopup.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); mResendMailPopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
然后我就对照着这样的方式试了一下
public class MyDialog extends DialogFragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //隐藏title getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //设置dialog的 进出 动画 getDialog().getWindow().setWindowAnimations(R.style.pop_bottom_anim); // 设置在屏幕的底部。 Window window = getDialog().getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); lp.gravity = Gravity.BOTTOM; window.setBackgroundDrawable(new ColorDrawable(0xff000000)); window.setAttributes(lp); DisplayMetrics dm = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm); getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height); //设置rootView View rootView = inflater.inflate(R.layout.dialog_bottom, null); return rootView; } }
然而并没有什么卵用。。。。
然后我就去度娘了
最后得到了答案….
@Override public void onStart() { super.onStart(); //解决不全屏的问题 DisplayMetrics dm = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm); getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height); }
必须在onstart方法里面设置这个属性,具体原因我就没深究了,工头又叫搬砖了,还是搬砖去吧……。
- DialogFragment的简单使用
- Android的DialogFragment的简单使用。
- android DialogFragment简单使用
- seekbar +DialogFragment 简单使用
- DialogFragment简单的使用方法
- Android:DialogFragment的使用
- ApiDemos--DialogFragment的使用
- DialogFragment的使用示例
- DialogFragment的相关使用
- Dialogfragment的使用
- Android-----DialogFragment的使用
- dialogfragment 的使用
- DialogFragment && NumberPicker的使用
- DialogFragment的使用
- DialogFragment的使用
- 一个简单的自定义DialogFragment
- DialogFragment和AlertDialog的使用
- DialogFragment的使用(自定义Dialog )
- 懒加载之jQuery图片延迟加载插件jQuery.lazyload
- Mybatis的一级缓存和二级缓存
- memcached 以及 session-server部署
- kylin : killed by admin
- NIO ServerSocketChannel
- DialogFragment的简单使用
- 机器学习之预测数值型数据: 回归
- 实验报告->数组的定义和使用->字符串逆置
- myeclips安装gradle插件
- C++基础
- nginx 001
- java到底是值传递还是引用传递
- Linux 字符串截取的八种方法
- 常用函数整理