//设置弹框窗口背景颜色透明
this.getWindow().setBackgroundDrawableResource(R.color.transparent);
this.getWindow().setGravity(Gravity.CENTER);
this.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
WindowManager.LayoutParams attributes = this.getWindow().getAttributes();
attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
attributes.height = WindowManager.LayoutParams.MATCH_PARENT;
this.getWindow().setAttributes(attributes);
要实现版本更新功能,弹框效果如下
update.jpg这个时候要让弹框窗口为透明色才能实现,弹框类如下
public class UpdateDialog extends Dialog {
public UpdateDialog(@NonNull Context context) {
super(context);
// mDialog = new UpdateDialog(context); //R.style.UpdateAlertDialog
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//加载布局文件
mLayout = inflater.inflate(R.layout.update_dialog, null, false);
//添加布局文件到 Dialog
this.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
//上边的小叉叉
xiaochachaImg = (ImageView)mLayout.findViewById(R.id.xiaochachaId);
//小叉叉下边的竖线
whiteLine = (View)mLayout.findViewById(R.id.xiaochachaWhiteLineId);
//最新版本
zuixinbanbenTextView = (TextView)mLayout.findViewById(R.id.zuixinbanbenTextViewId);
//新版本大小
xinbanbendaxiaoTextView = (TextView)mLayout.findViewById(R.id.xinbanbendaxiaoId);
//更新内容
gengxinneirongEditext = (EditText)mLayout.findViewById(R.id.gengxinneirongId);
//两个按钮的背景view
btnsBackId = (LinearLayout)mLayout.findViewById(R.id.dibuanniumenId);
//取消按钮
cancleBtn = (RelativeLayout) mLayout.findViewById(R.id.quxiaoanniuId);
//更新按钮和取消按钮中间的竖线
btnCenterLine = (View)mLayout.findViewById(R.id.quxiaoCenterLineId);
//更新按钮
updateBtn = (RelativeLayout)mLayout.findViewById(R.id.gengxinanniuId);
//进度条的背景view
jindutiaoBackId = (LinearLayout)mLayout.findViewById(R.id.jindutiaoBackId);
//进度条上面的文字
gengxinjinduTextView = (TextView)mLayout.findViewById(R.id.gengxinjinduId);
//进度条
progressBar = (ProgressBar)mLayout.findViewById(R.id.jindutiaoId);
xiaochachaImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
xiaochachaClickListener.onClick(v);
}
});
cancleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quxiaoClickListener.onClick(v);
}
});
updateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gengxinClickListener.onClick(v);
}
});
this.setContentView(mLayout);
this.setCancelable(true); //用户可以点击后退键关闭 Dialog
this.setCanceledOnTouchOutside(false); //用户不可以点击外部来关闭 Dialog
//设置弹框窗口背景颜色透明
this.getWindow().setBackgroundDrawableResource(R.color.transparent);
this.getWindow().setGravity(Gravity.CENTER);
this.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
WindowManager.LayoutParams attributes = this.getWindow().getAttributes();
attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
attributes.height = WindowManager.LayoutParams.MATCH_PARENT;
this.getWindow().setAttributes(attributes);
}
public UpdateDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
protected UpdateDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
private View mLayout;
private ImageView xiaochachaImg;
private View whiteLine;
private TextView zuixinbanbenTextView;
private TextView xinbanbendaxiaoTextView;
private EditText gengxinneirongEditext;
//按钮们
private LinearLayout btnsBackId;
private RelativeLayout cancleBtn;
private View btnCenterLine;
private RelativeLayout updateBtn;
//进度条
private LinearLayout jindutiaoBackId;
private TextView gengxinjinduTextView;
private ProgressBar progressBar;
//点击小叉叉
private View.OnClickListener xiaochachaClickListener;
//点击取消按钮
private View.OnClickListener quxiaoClickListener;
//点击更新按钮
private View.OnClickListener gengxinClickListener;
private UpdateDialog mDialog;
/*强制更新隐藏小叉叉按钮*/
public void feiqiangzhigengxin(){
xiaochachaImg.setVisibility(View.GONE);
whiteLine.setVisibility(View.GONE);
cancleBtn.setVisibility(View.GONE);
btnCenterLine.setVisibility(View.GONE);
}
/*小叉叉的点击事件*/
public void setXiaochachaListen(View.OnClickListener listener){
xiaochachaClickListener = listener;
}
/*取消按钮的点击事件*/
public void setCancneListen(View.OnClickListener listener){
quxiaoClickListener = listener;
}
/*更新按钮的点击事件*/
public void setUpdateListen(View.OnClickListener listener){
gengxinClickListener = listener;
}
/*设置最新版本号*/
public void setZuixinbanben(String banbenhao){
zuixinbanbenTextView.setText(banbenhao);
}
/*设置新版本大小*/
public void setXinbanbendaxiao(String daxiao){
xinbanbendaxiaoTextView.setText(daxiao);
}
/*更新内容*/
public void setGengxinneirong(String neirong){
gengxinneirongEditext.setText(neirong);
}
/*更新进度*/
public void setGengxinjindu(int jindu){
progressBar.setProgress(jindu);
gengxinjinduTextView.setText("更新中("+String.valueOf(jindu)+"%)");
}
/*点击立即更新隐藏按钮出现进度条*/
public void setXiazaizhong(){
btnsBackId.setVisibility(View.GONE);
jindutiaoBackId.setVisibility(View.VISIBLE);
xiaochachaImg.setVisibility(View.GONE);
whiteLine.setVisibility(View.GONE);
}
}
弹框布局如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/xiaochachaId"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@mipmap/update_xiaochacha_img"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
/>
<View
android:id="@+id/xiaochachaWhiteLineId"
android:layout_width="0.5dp"
android:layout_height="50dp"
android:background="#fff"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:layout_below="@+id/xiaochachaId"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_marginTop="40dp"
android:src="@mipmap/update_top_img"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#fff"
android:paddingHorizontal="15dp"
android:paddingTop="10dp"
android:layout_marginTop="-5dp">
<TextView
android:id="@+id/zuixinbanbenTextViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#333"
android:textSize="16dp"
android:text="最新版本:V1.0.0"/>
<TextView
android:id="@+id/xinbanbendaxiaoId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#333"
android:textSize="16dp"
android:text="新版本大小:10.5M"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical"
android:background="#fff"
android:paddingHorizontal="15dp"
android:paddingVertical="20dp"
>
<EditText
android:id="@+id/gengxinneirongId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:focusable="false"
android:textCursorDrawable="@color/transparent"
android:textColor="#999"
android:textSize="16dp"
android:text="更新内容:"/>
</LinearLayout>
<!--取消按钮和更新按钮-->
<LinearLayout
android:id="@+id/dibuanniumenId"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="vertical"
android:background="@drawable/update_dialog_btn_back_style"
android:visibility="visible">
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#e5e5e5"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<RelativeLayout
android:id="@+id/quxiaoanniuId"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:textColor="#999999"
android:textSize="18dp"
android:text="取消"
/>
</RelativeLayout>
<View
android:id="@+id/quxiaoCenterLineId"
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#e5e5e5"/>
<RelativeLayout
android:id="@+id/gengxinanniuId"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/main_color_light"
android:textSize="18dp"
android:gravity="center"
android:text="立即更新"
android:layout_centerInParent="true"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<!--进度条-->
<LinearLayout
android:id="@+id/jindutiaoBackId"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="vertical"
android:background="@drawable/update_dialog_btn_back_style"
android:paddingHorizontal="15dp"
android:visibility="gone">
<TextView
android:id="@+id/gengxinjinduId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#333"
android:textSize="12dp"
android:text="更新中(0%)"/>
<ProgressBar
android:id="@+id/jindutiaoId"
android:layout_width="match_parent"
android:layout_height="20dp"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:progress="0"
android:progressBackgroundTint="#999"
android:progressTint="@color/main_color_light"
/>
</LinearLayout>
<!--因为上边内容透明,总体感觉弹框偏下,这里加一个透明组件网上顶一顶-->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="50dp"/>
</LinearLayout>
</RelativeLayout>
使用方法
updateDialog = new UpdateDialog(context);
updateDialog.setZuixinbanben("最新版本:V"+verName);
updateDialog.setXinbanbendaxiao("新版本大小:"+updateDao.getData().getFileSize()+"M");
updateDialog.setGengxinneirong(updateDao.getData().getUpdateContent());
updateDialog.setXiaochachaListen(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateDialog.cancel();
}
});
updateDialog.setCancneListen(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateDialog.cancel();
}
});
updateDialog.setUpdateListen(new View.OnClickListener() {
@Override
public void onClick(View v) {
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/updateApkFile/zhihuiCrm.apk";
File apkFile = new File(filePath);
if (apkFile.exists()==true){
apkFile.delete();//如果存在就删掉
Log.d("","删除已经存在的安装包");
}
updateDialog.setXiazaizhong();
downFile(updateDao.getData().getUrl(), context);
}
});
updateDialog.show();
参考文章
https://www.cnblogs.com/cloudfloating/p/9811380.html
https://www.jianshu.com/p/8f3db94d7efe
网友评论