自定义弹窗

效果图:


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);
    }



源码






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值