1.MyDialog代码:
public class MyDialog{private Activity activity;private ViewGroup contentView;private View view;public MyDialog(Activity activity) {this.activity=activity;contentView = activity.findViewById(android.R.id.content);view=LayoutInflater.from(activity).inflate(R.layout.dialog_layout,null);view.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dismiss();}});view.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dismiss();}});}/*** 显示dialog(包含动画)*/public void show(){Animation animation= AnimationUtils.loadAnimation(activity,R.anim.dialog_in_anim);view.setAnimation(animation);contentView.addView(view);}/*** 移除dialog(包含动画)*/public void dismiss(){Animation animation= AnimationUtils.loadAnimation(activity,R.anim.dialog_out_anim);view.setAnimation(animation);contentView.removeView(view);}
}
2.res/anim 下新建 dialog_in_anim.xml 和 dialog_out_anim.xml
dialog_in_anim.xml 底部显示
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="300"android:fromYDelta="100%p"android:toYDelta="0%p" />
</set>
dialog_out_anim.xml 底部隐藏
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="300"android:fromYDelta="0%p"android:toYDelta="100%p" />
</set>
3.界面布局:dialog_layout.xml ,半透明颜色值:#80000000
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#80000000"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:background="@android:color/white"android:orientation="vertical"android:paddingStart="40dp"android:paddingTop="20dp"android:paddingEnd="40dp"android:paddingBottom="20dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_marginBottom="30dp"android:text="标题"android:textColor="@android:color/black"android:textSize="24sp"android:textStyle="bold" /><LinearLayoutandroid:layout_width="120dp"android:layout_height="200dp"android:layout_gravity="center"android:layout_marginBottom="20dp"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginStart="30dp"android:orientation="horizontal"><TextViewandroid:id="@+id/tv_cancel"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:background="@android:color/darker_gray"android:gravity="center"android:paddingStart="60dp"android:paddingTop="15dp"android:paddingEnd="60dp"android:paddingBottom="15dp"android:text="关闭"android:textColor="@android:color/white"android:textSize="18sp" /><TextViewandroid:id="@+id/tv_sure"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:color/holo_orange_dark"android:gravity="center"android:paddingStart="60dp"android:paddingTop="15dp"android:paddingEnd="60dp"android:paddingBottom="15dp"android:layout_marginStart="20dp"android:text="确定"android:textColor="@android:color/white"android:textSize="18sp" /></LinearLayout></LinearLayout>
</FrameLayout>
4.使用:
new MyDialog(activity).show();