一、布局文件
<?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="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#333333"
android:gravity="center"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleInverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminateBehavior="repeat"
android:indeterminateDrawable="@drawable/anim_loading"
android:background="@android:color/transparent" />
<TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="10dp"
android:text="Message"
android:textColor="@color/white"
tools:ignore="HardcodedText" />
</LinearLayout>
</LinearLayout>
二、在res下drawable文件夹,新建anim_loading.xml文件
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/icon_loading"
android:fromDegrees="0.0"
android:pivotX="50.0%"
android:pivotY="50.0%"
android:toDegrees="360.0" />
三、在res下drawable文件夹下新建dialog_round.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!--shape用于定义形状,有四种形状(矩形rectangle| 椭圆oval | 直线line | 圆形ring)-->
<!--solid用于设置填充形状的颜色-->
<!--corners用于创建圆角(只用于形状是矩形)-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#333333"/>
<corners
android:topLeftRadius="8dip"
android:topRightRadius="8dip"
android:bottomLeftRadius="8dip"
android:bottomRightRadius="8dip"/>
</shape>
四、解决圆角宽度留白问题
新建style样式,在构造的时候调用即可:super(context, R.style.LoadProgressDialog);
<style name="LoadProgressDialog" parent="Theme.AppCompat.Dialog">
<item name="android:backgroundDimEnabled">true</item><!-- 模糊 -->
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimAmount">0.6</item><!-- 灰度 -->
<!--Dialog图标着色-->
<!--<item name="colorAccent">#ffffff</item>-->
</style>
五、新建LoadProgressDialog 继承Dialog
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class LoadProgressDialog extends Dialog {
private String message;
private boolean canCancel;
private TextView textView;
public LoadProgressDialog(Context context, String message) {
this(context, message, true);
}
public LoadProgressDialog(Context context, String message, boolean canCancel) {
super(context, R.style.LoadProgressDialog);
this.message = message;
this.canCancel = canCancel;
}
public void setMessage(String message) {
this.message = message;
handler.sendEmptyMessage(0);
}
@SuppressLint("HandlerLeak")
private
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
textView.setText(message);
}
}
};
@SuppressLint("ResourceType")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_loadprogress);
textView = findViewById(R.id.tv_message);
setCancelable(canCancel);
setCanceledOnTouchOutside(canCancel);
textView.setText(message);
}
}
六、使用示例
LoadProgressDialog loadProgressDialog=new LoadProgressDialog(DemoActivity.this,"登录中……",false);
//显示
loadProgressDialog.show();
//更改message
loadProgressDialog.setMessage("加载数据中……");
//销毁
loadProgressDialog.dismiss();