自定义DialogFragment实现复杂Dialog
来源:互联网 发布:gz java 编辑:程序博客网 时间:2024/06/05 00:10
- What
- Why
- How
- 继承DialogFragment并重写onCreateDialog方法
- 继承AbstractCustomDialogFragment重写onCreateDialogView方法
- 在调用的地方调用
- 对应的fragment_my_dialogxml文件
What
一种更加灵活的Dialog
Why
谷歌推荐,但是有时不能够满足需求,比如定制边距,突出关闭按钮,当然你也可以使用Dialog形式的Activity
目标效果:
How
1. 继承DialogFragment并重写onCreateDialog方法
public abstract class AbstractCustomDialogFragment extends DialogFragment{ @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Dialog alertDialog = new Dialog(getContext()); alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); View customView = onCreateDialogView(); alertDialog.setContentView(customView); alertDialog.setCanceledOnTouchOutside(false); alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); WindowManager.LayoutParams windowParams = alertDialog.getWindow().getAttributes(); windowParams.width = (int) (getResources().getDisplayMetrics().widthPixels - getResources().getDisplayMetrics().density * 20); alertDialog.getWindow().setAttributes(windowParams); return alertDialog; } //提供接口用来获取Dialog样式 public abstract View onCreateDialogView();}
2.继承AbstractCustomDialogFragment,重写onCreateDialogView方法
public class MyDialogFragment extends AbstractCustomDialogFragment { private View mView; @Override public View onCreateDialogView() { mView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_my_dialog, null); mView.findViewById(R.id.imageView).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dismiss(); } }); return mView; }}
3.在调用的地方调用
MyDialogFragment dialogFragment = new MyDialogFragment();dialogFragment.show(getSupportFragmentManager(), "FirstDialog");
4.对应的fragment_my_dialog.xml
文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/transparent" tools:context="study.ung.maitamaresearch.dialogs.MyDialogFragment"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="@dimen/margin_10dp" android:background="@color/transparent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="@string/hello_blank_fragment" /> </RelativeLayout> <ImageView android:id="@+id/imageView" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:src="@drawable/close" /></RelativeLayout>
阅读全文
0 0
- 自定义DialogFragment实现复杂Dialog
- 自定义 DialogFragment 实现底部弹出 dialog
- 自定义Dialog&DialogFragment
- 自定义Dialog和DialogFragment
- 自定义dialog 通过DialogFragment
- android 使用DialogFragment 自定义Dialog
- DialogFragment的使用(自定义Dialog )
- DialogFragment实现自定义进度条
- DialogFragment自定义dialog的位置和大小
- dialogFragment---dialog
- Android-->Dialog/DialogFragment宽度高度修改/全屏,自定义样式
- Android-->Dialog/DialogFragment宽度高度修改/全屏,自定义样式
- 自定义dialogfragment实现底部弹出框效果
- DialogFragment实现自定义布局的小技巧
- Android 使用DialogFragment 实现一个可以多选的Dialog
- 自定义DialogFragment
- 自定义DialogFragment
- 自定义DialogFragment
- 面向GNU/Linux桌面环境的KDE Plasma 5.11正式发布
- windows下架设SVN服务器并设置开机启动
- TPC-H DBGEN使用方法_ 生成数据表(Linux_Windows)
- DFS-lintcode单词搜索(word-search)
- 技术文档汇总
- 自定义DialogFragment实现复杂Dialog
- OpenCV之路(三)
- RecyclerView初级
- JACO2 6自由度机械臂上手体验一
- curl命令
- 【动态规划】BZOJ2423: [HAOI2010]最长公共子序列
- 构造代码块与静态代码块(静态初始化块)
- 模型汇总23
- SQL语句中的<>和!=