美文网首页
【Android】自定义dialog

【Android】自定义dialog

作者: renkuo | 来源:发表于2019-05-31 16:10 被阅读0次
    自定义dialog类
    import android.app.AlertDialog;
    import android.content.Context;
    import android.graphics.PixelFormat;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.WindowManager;
    import android.widget.Button;
    
    import com.douyu.lib.utils.DYDensityUtils;
    import com.douyu.module.screencast.manager.SCCastManager;
    
    import tv.douyu.plugin.demo.SCApplication;
    import tv.douyu.plugin.screencast.R;
    
    public class ExitDialog extends AlertDialog {
    
        private Context mContext;
        private Button btnOk, btnCancel;
    
        protected ExitDialog(Context context) {
            super(context, R.style.exit_cast_activity_style);
            this.mContext = context;
        }
    
        protected ExitDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
            super(context, cancelable, cancelListener);
            this.mContext = context;
        }
    
        protected ExitDialog(Context context, int themeResId) {
            super(context, themeResId);
            this.mContext = context;
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            View view = LayoutInflater.from(SCApplication.getInstance().getApplicationContext()).inflate(R.layout.sc_exit_dialog, null);
            setContentView(view);
            initView(view);
            initListener();
        }
    
        private void initListener() {
    
            btnOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //断开连接逻辑
    
                    SCCastManager.getInstance().removeCastControlView();
                    SCCastManager.getInstance().exitAllFloatVIew();
                    cancel();
    
                }
            });
    
            btnCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancel();
                }
            });
        }
    
        private void initView(View view) {
            btnOk = (Button) view.findViewById(R.id.btn_ok);
            btnCancel = (Button) view.findViewById(R.id.btn_cancel);
        }
    
        @Override
        public void show() {
            super.show();
            WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
            layoutParams.gravity = Gravity.CENTER;
            layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
            layoutParams.gravity = Gravity.CENTER;
            layoutParams.format = PixelFormat.TRANSLUCENT;
            layoutParams.width = DYDensityUtils.dip2px(289);
            layoutParams.height = DYDensityUtils.dip2px(181);
            getWindow().setAttributes(layoutParams);
        }
    }
    
    dialog样式
    <style name="exit_cast_activity_style" parent="@android:style/Theme.Dialog">
            <item name="android:windowBackground">@color/lib_transparent</item>
            <!--//设置dialog的背景,此处为系统给定的透明值 -->
            <item name="android:windowFrame">@null</item>
            <!--//Dialog的windowFrame框为无 -->
            <item name="android:windowNoTitle">true</item>         <!--//是否显示标题-->
            <item name="android:windowIsFloating">true</item>
            <!--//是否浮现在activity之上 -->
            <item name="android:windowIsTranslucent">false</item>
            <!--//是否半透明 -->
            <item name="android:windowContentOverlay">@null</item>
            <!--//是否有覆盖 -->
            <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
            <!--//设置Activity出现方式 -->
            <item name="android:backgroundDimEnabled">false</item>
            <!--//背景是否模糊显示 -->
        </style>
    
    调用方式
    private void handleClickClose() {
            if (mExitDialog == null){
                mExitDialog = new ExitDialog(mContext);
            }
            mExitDialog.show();
        }
    
    dialog设置宽度全屏,要设置在show的后面
     @Override
        public void show() {
            super.show();
    /**
     * 设置宽度全屏,要设置在show的后面
     */
            WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
            layoutParams.gravity=Gravity.BOTTOM;
            layoutParams.width= LayoutParams.MATCH_PARENT;
            layoutParams.height= LayoutParams.WRAP_CONTENT;
            getWindow().getDecorView().setPadding(0, 0, 0, 0);
            getWindow().setAttributes(layoutParams);
        }
    

    错误不足之处或相关建议欢迎大家评论指出,谢谢!如果觉得内容可以的话麻烦喜欢(♥)一下

    相关文章

      网友评论

          本文标题:【Android】自定义dialog

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