downLoad.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="640dp"
android:layout_height="260dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FF0A0A0A"
android:gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progressBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="486dp"
android:layout_height="6dp"
android:progressDrawable="@drawable/progress_bar" />
<TextView
android:id="@+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textColor="#DDDDDD"
android:background="@android:color/transparent"
tools:text="2.3MB / 15MB" />
</LinearLayout>
<TextView
android:id="@+id/tv_close"
android:layout_width="match_parent"
android:layout_height="72dp"
android:gravity="center"
android:text="@string/close"
android:textColor="#DDDDDD"
android:background="#0ADDDDDD"
android:textSize="26sp" />
</LinearLayout>
downLoad.java
package com.geely.util;
import android.app.AlertDialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.geely.activity.DownloadFragment;
import com.geely.main.R;
public class DownLoadDialog extends AlertDialog {
TextView tv_progress;
ProgressBar progressBar;
TextView tv_close;
private int max;
private String mProgressNumberFormat;
public DownLoadDialog(Context context) {
super(context);
}
private void initView() {
getWindow().setDimAmount(0.6f);
tv_progress = findViewById(R.id.tv_progress);
progressBar = findViewById(R.id.progressBar);
tv_close = findViewById(R.id.tv_close);
tv_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DownLoadDialog.this.dismiss();
DownloadFragment.flag = false;
if(DownloadFragment.buttonDown!=null){
DownloadFragment.buttonDown.setEnabled(true);
}
}
});
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
progressBar.setMax(max);
}
public void setProgress(int progress) {
progressBar.setProgress(progress);
tv_progress.setText(String.format(mProgressNumberFormat,progress,max));
}
@Override
public void setContentView(@NonNull View view) {
super.setContentView(view);
initView();
}
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
initView();
}
public void setProgressNumberFormat(String s) {
mProgressNumberFormat = s;
}
}
3.使用
private DownLoadDialog downLoadDialog;
downLoadDialog.setProgressNumberFormat("%1d M/%2d M");
downLoadDialog = new DownLoadDialog(getActivity());
downLoadDialog.setCancelable(false);//设置为不可关闭
downLoadDialog.show();
downLoadDialog.setContentView(R.layout.layout_dialog_download);
downLoadDialog.setMax(size);