8 Android课程学习

16 篇文章 0 订阅

自定义Dialog
1.继承dialog类
2.布局文件
3.自定义样式
4.Java代码使用
自定义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样式

调用方式

private void handleClickClose() {
if (mExitDialog == null){
mExitDialog = new ExitDialog(mContext);
}
mExitDialog.show();
}
1
2
3
4
5
6
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);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值