layout/activity_animat_dialog.xml <RelativeLayout xmlns:android="http://schemas./apk/res/android" xmlns:tools="http://schemas./tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".AnimatDialogActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:text="@string/hello_world" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="點擊我彈出對話框" /> </RelativeLayout> values/styles.xml <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> anim/dialog_enter.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas./apk/res/android" > <!-- %p指相對于父容器 android:fromYDelta="100%p" android:toXDelta="100%p"--> <translate android:duration="600" android:fromXDelta="100%p" /> </set> anim/dialog_exit.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas./apk/res/android" > <!-- //持續(xù)時間 --> <translate android:duration="600" android:toXDelta="100%" /> </set> AnimatDialogActivity.java public class AnimatDialogActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_animat_dialog); button=(Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub AlertDialog dialog = new AlertDialog.Builder(AnimatDialogActivity.this) .setTitle("title").setMessage("message").create(); Window window = dialog.getWindow(); window.setGravity(Gravity.LEFT); //此處可以設(shè)置dialog顯示的位置 window.setWindowAnimations(R.style.mystyle); //添加動畫 dialog.show(); } }); } } |
|
來自: 昵稱15103532 > 《待分類1》