Android实现从底部弹出的对话框的方法

  介绍

这篇文章将为大家详细讲解有关Android实现从底部弹出的对话框的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

1。点击按钮(按钮的点击事件在此不在赘述,接下来直接写底部弹框的实现方式和样式的设计)

2。弹框

Dialog  Dialog =, new 对话框(上下文,R.style.ActionSheetDialogStyle);   ,,,//填充对话框的布局   ,,,inflate =, LayoutInflater.from(上下文).inflate (R.layout.dialog_layout, null);   ,,,//,setCancelable (iscancelable);//点击外部不可忽略   ,,,//setCanceledOnTouchOutside (isBackCanCelable);   ,,,//初始化控件   ,,,spinner =,(旋转),inflate.findViewById (R.id.sp);   ,,,beizhu =, (TextView), inflate.findViewById (R.id.beizhu);   ,,,btn_cancel =,(按钮),inflate.findViewById (R.id.btn_cancel);   ,,,btn_ok =,(按钮),inflate.findViewById (R.id.btn_ok);   ,,,//将布局设置给对话框   ,,,taskProgress.setContentView(膨胀);   ,,,//获取当前活动所在的窗体   ,,,Window  dialogWindow =, taskProgress.getWindow ();   ,,,//设置对话框从窗体底部弹出   ,,,dialogWindow.setGravity (Gravity.BOTTOM);   ,,,//获得窗体的属性   ,,,WindowManager.LayoutParams  lp =, dialogWindow.getAttributes ();   ,,,//如果没有这行代码,弹框的内容会自适应,而不会充满父控件   ,,,lp.width =, WindowManager.LayoutParams.MATCH_PARENT;   ,,,lp.y =, 40;//设置对话框距离底部的距离   ,,,//将属性设置给窗体   ,,,dialogWindow.setAttributes (lp);   ,,,dialog ,告诉();//显示对话框   ,,,在需要消失地方直接   ,,,dialog.dismiss ();

3。窗口的样式

& lt; style  name=癆ctionSheetDialogStyle",父母=癅android:风格/Theme.Dialog"比;      & lt; !——,才能背景透明,——比;   & lt;才能item  name=癮ndroid: windowBackground"祝辞@android:颜色/transparent   & lt;才能item  name=癮ndroid: windowContentOverlay"祝辞@null   & lt;才能!——,浮于活动之上,——比;   & lt;才能item  name=癮ndroid: windowIsFloating"祝辞true   & lt;才能!——,边框,——比;   & lt;才能item  name=癮ndroid: windowFrame"祝辞@null   & lt; !——,才能对话框以外的区域模糊效果,——比;   & lt;才能item  name=癮ndroid: backgroundDimEnabled"祝辞true   & lt; !——,才能无标题,——比;   & lt;才能item  name=癮ndroid: windowNoTitle"祝辞true   & lt;才能!——,半透明,——比;   & lt;才能item  name=癮ndroid: windowIsTranslucent"祝辞true   & lt; !——,才能对话框进入及退出动画,——比;   & lt;才能item  name=癮ndroid: windowAnimationStyle"祝辞@style/ActionSheetDialogAnimation   ,& lt;/style>   ,& lt; !——, ActionSheet进出动画,——比;   ,& lt; style  name=癆ctionSheetDialogAnimation",父母=癅android:风格/Animation.Dialog"比;   & lt;才能item  name=癮ndroid: windowEnterAnimation"祝辞@anim/actionsheet_dialog_in   & lt;才能item  name=癮ndroid: windowExitAnimation"祝辞@anim/actionsheet_dialog_out   ,& lt;/style>

4。窗口出现和消失的效果

对话框出现动画代码:

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; translate  xmlns: android=癶ttp://schemas.android.com/apk/res/android"   ,android:持续时间=?00”;   ,android: fromYDelta=?00%”;   ,android: toYDelta=?“,/在

对话框消失的代码:

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; translate  xmlns: android=癶ttp://schemas.android.com/apk/res/android"   ,android:持续时间=?00”;   ,android: fromYDelta=?”;   ,android: toYDelta=?00%“,/在

5。弹框的整体布局

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; LinearLayout  xmlns: android=癶ttp://schemas.android.com/apk/res/android"   ,xmlns:工具=癶ttp://schemas.android.com/tools"   ,android: id=癅 + id/activity_task_progress"   ,android: layout_width=癿atch_parent"   ,android: layout_height=癿atch_parent"   ,android:重力=癱enter"   ,android:取向=皏ertical"祝辞   & lt; LinearLayout   android:才能layout_width=癿atch_parent"   android:才能layout_height=皐rap_content"   android:才能layout_margin=?0 dp"   android:才能背景=癅drawable/lin_style"   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Android实现从底部弹出的对话框的方法