在平时的开发中,经常遇到一些使用全屏弹窗的场景。今天,总结一下全屏弹窗的几种实现方法,大家可以结合自己的使用场景和习惯选择合适的方法。
实现全屏弹窗,我总结了三种方法:
(1)全屏Dialog
(2)全屏DialogFragment
(3)全屏Activity
在一般的情况下,使用Dialog即可,相信大家实现弹窗的第一选择都是Dialog,这也是最简单的方式。其实,大家可以尝试一下DialogFragment,除了能实现Dialog的界面和功能外,因为它本身是Fragment,所以,它有自己的生命周期管理。这里需要注意一点,DialogFragment只能在Activity中去使用。至于全屏Activity,其实它的实现与前两种方式差不多。但是,在有些场景下,或许使用全屏Activity实现弹窗是更合适的方法。大家都知道,我们弹Dialog是需要依附一个Activity的。假如我们要依附的Activity处于非alive状态,那么我们无法弹出Dialog。当然,也可以通过弹出悬浮窗的方式弹系统窗口。但是,这是另外一种比较流氓的场景,我们不多介绍。
public class FullScreenDialog extends Dialog {
private TextView mBtn1;
private TextView mBtn2;
private ImageView mClose;
private OnDialogClickListener mListener;
public FullScreenDialog(@NonNull Context context) {
super(context, R.style.FullScreenDialog);
initView(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WindowManager m = getWindow().getWindowManager();
Display d = m.getDefaultDisplay();
WindowManager.LayoutParams p = getWindow().getAttributes();
p.height = d.getHeight();
p.width = d.getWidth();
getWindow().setAttributes(p);
}
public void initView(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.dialog_layout, null);
setContentView(view);
setCancelable(false);
setCanceledOnTouchOutside(false);
mBtn1 = view.findViewById(R.id.tv_1);
mBtn2 = view.findViewById(R.id.tv_2);
mClose = view.findViewById(R.id.close);
mBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mListener != null) {
mListener.onBtn1Click();
}
dismiss();
}
});
mBtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mListener != null) {
mListener.onBtn2Click();
}
dismiss();
}
});
mClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
}
public void setOnDialogClickListener(OnDialogClickListener listener) {
mListener = listener;
}
public interface OnDialogClickListener {
void onBtn1Click();
void onBtn2Click();
}
}
public class FullScreenDialogFragment extends DialogFragment {
private TextView mBtn1;
private TextView mBtn2;
private ImageView mClose;
private FullScreenDialog.OnDialogClickListener mListener;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final Window window = getDialog().getWindow();
if (window != null) {
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.requestFeature(Window.FEATURE_NO_TITLE);
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
}
setCancelable(false);
View view = inflater.inflate(R.layout.dialog_layout, container, false);
initView(view);
return view;
}
@Override
public void onResume() {
super.onResume();
final Window window = getDialog().getWindow();
if (window != null) {
WindowManager.LayoutParams params = window.getAttributes();
params.height = WindowManager.LayoutParams.MATCH_PARENT;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.gravity = Gravity.TOP;
window.setAttributes(params);
}
}
private void initView(View view) {
mBtn1 = view.findViewById(R.id.tv_1);
mBtn2 = view.findViewById(R.id.tv_2);
mClose = view.findViewById(R.id.close);
mBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mListener != null) {
mListener.onBtn1Click();
}
dismiss();
}
});
mBtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mListener != null) {
mListener.onBtn2Click();
}
dismiss();
}
});
mClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
}
public void setOnDialogClickListener(FullScreenDialog.OnDialogClickListener listener) {
mListener = listener;
}
public interface OnDialogClickListener {
void onBtn1Click();
void onBtn2Click();
}
}
public class FullScreenDialogActivity extends AppCompatActivity {
private TextView mBtn1;
private TextView mBtn2;
private ImageView mClose;
public static void startActivity(Context context){
Intent intent = new Intent();
intent.setClass(context,FullScreenDialogActivity.class);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_layout);
final Window window = getWindow();
if (window != null) {
WindowManager.LayoutParams params = window.getAttributes();
params.height = WindowManager.LayoutParams.MATCH_PARENT;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.gravity = Gravity.BOTTOM;
window.setAttributes(params);
}
initView();
}
private void initView() {
mBtn1 = findViewById(R.id.tv_1);
mBtn2 = findViewById(R.id.tv_2);
mClose = findViewById(R.id.close);
mBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
mBtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
mClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
通过比较这三种方式的代码,我们可以发现,其实最根本的就是设置window的一些参数和属性,而这三种方式会有一点差异。相信代码不难理解,我也不过多的介绍了。