自定义PopupWindow和AlertDialog

PopupWindow是一个常用的一个组件,和popwindow相似的组件,还有一个叫AlertDialog

PopupWindow的自定义相信大家一定很熟悉,这里我展示一下我的相关代码了,如有不足请指教哦

PopupWindow pop=new PopupWindow(context);
        View contentView = LayoutInflater.from(context).inflate(R.layout.navigationpopup, null);  //获取popupwindow样式
        pop.setContentView(contentView);
        pop.setWidth(LayoutParams.MATCH_PARENT);// 设置宽度
        pop.setHeight(LayoutParams.WRAP_CONTENT);//设置高度
        //初始化layout 上面的view
        Button btn=(Button)contentView.findViewById(R.id.btn);
        //添加点击事件--省略
pop.setAnimationStyle(R.style.AnimationFade_bottom);//设置动画
        pop.setOutsideTouchable(true);//设置outside touch
        pop.getBackground().setAlpha(0);//设置背景为透明
        pop.showAtLocation(context.findViewById(R.id.ll_web), Gravity.BOTTOM, 0, 0);//设置显示

弹出动画展示给大家:

<style name="AnimationFade_bottom">

        <!-- PopupWindow_bottom上下弹出的效果 -->
        <item name="android:windowEnterAnimation">@anim/in_bottomtotop</item>
        <item name="android:windowExitAnimation">@anim/out_toptobottom</item>
    </style>

@anim/in_bottomtotop

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定义从上向下进入的动画 -->
    <translate
        android:duration="200"
        android:fromYDelta="100%"
        android:toYDelta="0" />

</set>

@anim/out_toptobottom

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定义从下向上进入的动画 -->
    <translate
        android:duration="200"
        android:fromYDelta="0"
        android:toYDelta="100%" />

</set>

自定义的样式主要看你的layout怎么写了

这里再写一个自定义的AlertDialog,

这里我们注意到Layout的作用,当我们自定义Alert 的时候就能够对于Alert的样式进行修改,从而达到自定义的目的。

// 取得自定义View    
        LayoutInflater layoutInflater = LayoutInflater.from(this);   
        View myLoginView = layoutInflater.inflate(R.layout.login, null);   

        Dialog alertDialog = new AlertDialog.Builder(this).   
                setTitle("用户登录").   
                setIcon(R.drawable.ic_launcher).   
                setView(myLoginView).   
                setPositiveButton("登录", new DialogInterface.OnClickListener() {   

                    @Override   
                    public void onClick(DialogInterface dialog, int which) {   
                        // TODO Auto-generated method stub    
                    }   
                }).   
                setNegativeButton("取消", new DialogInterface.OnClickListener() {   

                    @Override   
                    public void onClick(DialogInterface dialog, int which) {   
                        // TODO Auto-generated method stub    
                    }   
                }).   
                create();   
        alertDialog.show();  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值