button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub AlertDialog dialog = new AlertDialog.Builder(TestAndroid1Activity.this) .setTitle("title").setMessage("message").create(); Window window = dialog.getWindow(); window.setGravity(Gravity.BOTTOM); //此處可以設定dialog顯示的位置 window.setWindowAnimations(R.style.mystyle); //添加動畫 dialog.show(); } });
styles.xml
<?xml version="1.0" encoding="utf-8"?> <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>
位於 res/anim/dialog_enter.xml<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="100%p" %p指相對於父容器 android:duration="600" /> </set>
位於 res/anim/dialog_exit.xml<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:toYDelta="100%p" android:duration="600" //期間 /> </set>