自定义底部滑出的dialog

作者: 一个厨子 | 来源:发表于2015-06-05 22:58 被阅读3227次

    目的:实现一个自定义dialog,用于退出,从底部滑入和滑出

    要解决的问题:

    1.dialog的位置要修改到底部;

    2.dialog有默认的padding,要去这些padding,让dialog于屏幕同宽;(这个无法通过设置Gravity.width办到)

    3.dialog的滑入和滑出动画;(style中有enter和exit Animation属性)

    解决方案:

    示例:写一个LogoutDialog继承自Dialog类

    关于问题1和2:

        Window window = this.getWindow();
        
        //去掉dialog的title,要在setContentView()前
        window.requestFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.layout_logout_confirm);
        
        //去掉dialog默认的padding
        window.getDecorView().setPadding(0, 0, 0, 0);
        
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        
        //设置dialog的位置在底部
        lp.gravity = Gravity.BOTTOM;
        
        window.setAttributes(lp);
    

    给dialog加入动画

        给dialog设置一个style,在style中设置,也可以在代码中写(但是相对繁琐)。
        
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.windowAnimations = R.style.MyDialogAnimation;//设置Animation,style中做了设置,此处可不写
    

    style :

    <style name="MyDialogStyle" parent="@android:style/Theme.Dialog">
        <item name="android:windowAnimationStyle">@style/MyDialogAnimation</item>
    </style>
    
    <style name="MyDialogAnimation">
        <item name="android:windowEnterAnimation">@anim/dialog_slide_up</item>
        <item name="android:windowExitAnimation">@anim/dialog_slide_down</item>
    </style>
    

    anim :

    dialog_slide_down:

    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
    <translate
        android:duration="300"
        android:fromYDelta="0"
        android:toYDelta="100%p"/>
    </set>
    

    dialog_slide_up:

    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
    <translate
        android:duration="300"
        android:fromYDelta="100%p"
        android:toYDelta="0"/>
    </set>
    

    完整代码:

    public class MyLogoutConfirmDialog extends Dialog implements View.OnClickListener {
        @ViewInject(id = R.id.logout_dialog_exit, click = true)
        RelativeLayout exitButton;
    
        @ViewInject(id = R.id.logout_dialog_cancel, click = true)
        RelativeLayout cancelButton;
    
        MyDialogClickListener myDialogClickListener;
    
        public MyLogoutConfirmDialog(Context context, MyDialogClickListener myDialogClickListener) {
            super(context, R.style.MyDialogStyle);
            this.myDialogClickListener = myDialogClickListener;
            init();
        }
    
        private void init() {
            Window window = this.getWindow();
            window.requestFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.layout_logout_confirm);
            ViewInjectUtils.initInjectedView(this, window.getDecorView());
            window.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.windowAnimations = R.style.MyDialogAnimation;
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.BOTTOM;
            window.setAttributes(lp);
            show();
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.logout_dialog_exit:
                    dismiss();
                    myDialogClickListener.exit();
                    break;
                case R.id.logout_dialog_cancel:
                    dismiss();
                    break;
            }
        }
    
        public interface MyDialogClickListener {
            void exit();
        }
    
    }
    

    THE END

    相关文章

      网友评论

      本文标题:自定义底部滑出的dialog

      本文链接:https://www.haomeiwen.com/subject/euwdqttx.html