点击事件是屏幕下方平移过来一个dialog
点击屏幕的其他地方能够恢复原来的样子
View view = (LinearLayout) getLayoutInflater().inflate(R.layout.inputphonenum, null); Button btn = (Button) view.findViewById(R.id.btn_send); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "TOAST", Toast.LENGTH_SHORT).show(); } }); AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setView(view) .create(); WindowManager.LayoutParams lp=dialog.getWindow().getAttributes(); lp.alpha=0.5f; dialog.getWindow().setAttributes(lp); Window window = dialog.getWindow(); window.setGravity(Gravity.BOTTOM); //此处可以设置dialog显示的位置 window.setWindowAnimations(R.style.mystyle); //添加动画 dialog.show();
inputphonenum====xml文件
<?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="match_parent" android:orientation="horizontal"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="123123123" /> <Button android:text="发送" android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
mystyle文件
<resources> <style name="mystyle" parent="android:Animation"> <item name="@android:windowEnterAnimation">@anim/dialog_enter</item> //进入时的动画 <item name="@android:windowExitAnimation">@anim/dialog_exit</item> //退出时的动画 </style> </resources>
dialog_enter
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="600" android:fromYDelta="100%" /> </set>
dialog_exit
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="600" android:toYDelta="100%p" />//持续时间 </set>
本文介绍如何在Android应用中实现一个从屏幕底部平移出现的Dialog,并附带进入和退出动画。通过设置对话框的Gravity为底部,并定义XML动画文件,可以轻松创建带有动画效果的弹出窗口。
2301

被折叠的 条评论
为什么被折叠?



