看到IOS端做了這個加載資料的效果,為了保持UI一緻,就自定義了Loading Dialog !
先看一下大概的效果:
下面就是代碼部分:
res/drawable/
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" >
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="20"
android:useLevel="false"
>
<gradient
android:centerColor="@color/colorAccent" // 主題控件顔色,可以根據需要給定
android:centerY="0.50"
android:endColor="@color/colorAccent"
android:startColor="#00ffffff"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>
Dialog主要布局:
layout/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_dialog"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="15dp"
android:paddingRight="25dp"
android:paddingLeft="25dp"
android:paddingBottom="5dp"
android:gravity="center">
<ProgressBar
android:id="@+id/pb_progress_bar"
android:layout_width="50dp"
android:layout_height="50dp"
android:indeterminateDrawable="@drawable/progressbar"
android:indeterminateDuration="700"
android:visibility="visible"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:text="New Text"
android:id="@+id/tv_loading"
android:layout_gravity="center_horizontal" />
</LinearLayout>
主要代碼部分:
java:
/**
*
* @param context
* @param isAlpha 是否需要透明度
* @param message 顯示加載的内容
* @return
*/
public static Dialog writeDialog(Context context,boolean isAlpha,String message){
Dialog progressDialog = new Dialog(context);
progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (isAlpha){
WindowManager.LayoutParams lp= progressDialog.getWindow().getAttributes();
lp.alpha=0.8f;
progressDialog.getWindow().setAttributes(lp);
}
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.dialog, null);
LinearLayout layout = (LinearLayout) v.findViewById(R.id.ll_dialog);
ProgressBar pb_progress_bar = (ProgressBar) v.findViewById(R.id.pb_progress_bar);
pb_progress_bar.setVisibility(View.VISIBLE);
TextView tv = (TextView) v.findViewById(R.id.tv_loading);
if (message==null||message.equals("")){
tv.setVisibility(View.GONE);
}else{
tv.setText(message);
//R.color.colorAccent 我是拿的主題控件顔色,可以根據需要給
tv.setTextColor(context.getResources().getColor(R.color.colorAccent)); } progressDialog.setContentView(layout, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); return progressDialog;}
Activity:
java:
progressDialog = DialogUtil.writeDialog(this,false,"正在加載");
progressDialog.show();
progressDialog.setCanceledOnTouchOutside(false);//設定點選螢幕加載框不會取消(傳回鍵可以取消)