一 、背景
QQ截图20180428154111.png使用自定义DialogFragment实现弹窗效果时,边缘透明图片作为背景图片,依然存在不透明背景;
修复后: QQ截图20180428153910.png
二、实现
因为项目中多个地方使用到DialogFragment,所以稍作了一下封装:
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.ButterKnife;
import butterknife.Unbinder;
/**
* Created by Ljt on 2018/4/27
*/
public abstract class BaseDislogFragment extends DialogFragment {
private boolean isShow = false;
private Unbinder unbinder;
public boolean isShow() {
return isShow;
}
public void setShow(boolean show) {
isShow = show;
}
/**
* 显示弹窗信息
* @param savedInstanceState
* @param builder
*/
protected abstract void displayDialog(Bundle savedInstanceState, AlertDialog.Builder builder);
/**
* 设置布局文件
* @return
*/
protected abstract int getLayoutId();
protected abstract boolean isCanceledOnTouchOutside();
protected abstract boolean isTransparent();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = super.onCreateView(inflater, container, savedInstanceState);
return rootView;
}
@Override
public void onStart() {
super.onStart();
if (isTransparent()) {
//设置背景半透明
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
//getDialog().setCancelable(false);//这个会屏蔽掉返回键
getDialog().setCanceledOnTouchOutside(isCanceledOnTouchOutside());
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(getLayoutId(), null);
unbinder = ButterKnife.bind(this, view);
builder.setView(view);
displayDialog(savedInstanceState,builder);
return builder.create();
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
public void dismissDialog() {
if (isShow) {
this.dismiss();
}
}
}
所以事实上,这里最终使用的还是使用onCreateDialog()方法返回AlertDialog来呈现自定义布局弹窗;
主要的实现背景透明效果,使用如下代码:
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
BaseDialogFragment的子类使用起来就只关注与具体差异的实现就好了:
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
//这里删除了部分带有APP报名路径的类的导入
import butterknife.BindView;
/**
* Created by Ljt on 2018/4/26
*/
public class RewardDialogFragment extends BaseDislogFragment {
@BindView(R.id.img_reward_bg)
ImageView imgRewardBg;
@BindView(R.id.tv_reward_sum)
TextView tvRewardSum;
@BindView(R.id.tv_reward_receive)
TextView tvRewardReceive;
@BindView(R.id.rl_reward_content)
RelativeLayout rlRewardContent;
@BindView(R.id.img_reward_close)
ImageView imgRewardClose;
private Callback callback;
public interface Callback {
void onClose();
void onReceive();
}
public void setCallback(Callback pCallBack){
this.callback = pCallBack;
}
public void show(FragmentManager fragmentManager) {
if (this.isAdded())return;
show(fragmentManager, "RewardDialogFragment");
setShow(false);
Settings.setFirstReward(false);
}
@Override
protected void displayDialog(Bundle savedInstanceState, android.support.v7.app.AlertDialog.Builder builder) {
imgRewardClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (callback != null)callback.onClose();
dismiss();
}
});
tvRewardReceive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (callback != null)callback.onReceive();
dismiss();
}
});
}
@Override
protected int getLayoutId() {
return R.layout.fragment_dialog_reward;
}
@Override
protected boolean isCanceledOnTouchOutside() {
return false;
}
@Override
protected boolean isTransparent() {
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
callback = null;
}
}
!!!请确认当中的AlertDialog使用android.support.v7.app.AlertDialog下的;否则在Android 5.0以下版本,依然是不透明。
网友评论