效果图:
1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="收藏"
android:background="@drawable/circle_bg"/>
<Button
android:layout_marginTop="5dp"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="关注"
android:background="@drawable/circle_bg"/>
<Button
android:layout_marginTop="5dp"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="转发"
android:background="@drawable/circle_bg"/>
<Button
android:layout_marginTop="5dp"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="取消"
android:background="@drawable/circle_bg"
android:textColor="#f00"/>
</LinearLayout>
2.定义弹窗动画
anim目录下
2.1 dialog_enter.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"//执行动画时间
android:fromYDelta="100%"
></translate>
</set>
2.2 dialog_exit.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:toYDelta="100%"
></translate>
</set>
styles
<style name="DialogAnimation" parent="android:Animation"
>
<item name="android:windowEnterAnimation" >@anim/dialog_enter</item>
<item name="android:windowExitAnimation">@anim/dialog_exit</item>
</style>
3.代码
在activity中写的方法,直接调用即可
private void showdialog() {
//建立弹窗
AlertDialog dialog = new AlertDialog.Builder(this).create();
Window w = dialog.getWindow();
//设置弹窗的位置
w.setGravity(Gravity.BOTTOM);
//给弹窗加滑出动画
w.setWindowAnimations(R.style.DialogAnimation);
//设置外围点击消失
dialog.setCanceledOnTouchOutside(true);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable());
dialog.show();//显示
View v = LayoutInflater.from(this).inflate(R.layout.dialog,null);
dialog.setContentView(v);//给弹窗加载布局
WindowManager wm = w.getWindowManager();
Display d = wm.getDefaultDisplay();//屏幕
WindowManager.LayoutParams p = w.getAttributes();//弹窗
p.width = (int)(d.getWidth()*0.5);//设置弹窗宽度为屏幕的一半
w.setAttributes(p);
}
源码