1. 程式人生 > >Android自定義網路資料載入等待框的簡單封裝

Android自定義網路資料載入等待框的簡單封裝

先上效果圖(在5.0以上機型顯示效果,如果是4.0會變為灰色):
這裡寫圖片描述

自定義Dialog如下:

public class LoadingCustom extends Dialog{
    private static LoadingCustom mLoadingProgress;

    public LoadingCustom(Context context) {
        super(context);

    }

    public LoadingCustom(Context context, int theme) {
        super(context, theme);
    }
    public
static void showprogress(Context context,CharSequence message,boolean iscanCancel){ mLoadingProgress=new LoadingCustom(context,R.style.loading_dialog);//自定義style檔案主要讓背景變成透明並去掉標題部分<!-- 自定義loading dialog --> <style name="loading_dialog" parent="Widget.AppCompat.ProgressBar"> <item name="android:windowNoTitle"
>true</item> <item name="android:background">@android:color/transparent</item> <!--彈出框時讓背景變暗--> <item name="android:backgroundDimEnabled">true</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent"
>true</item> </style> //觸控外部無法取消,必須 mLoadingProgress.setCanceledOnTouchOutside(false); mLoadingProgress.setTitle(""); mLoadingProgress.setContentView(R.layout.loading_layout); mLoadingProgress.getWindow().setBackgroundDrawableResource(android.R.color.transparent); if(message==null|| TextUtils.isEmpty(message)){ mLoadingProgress.findViewById(R.id.loading_tv).setVisibility(View.GONE); }else { TextView tv = (TextView) mLoadingProgress.findViewById(R.id.loading_tv); tv.setText(message); } //按返回鍵響應是否取消等待框的顯示 mLoadingProgress.setCancelable(iscanCancel); mLoadingProgress.show(); } public static void dismissprogress(){ if(mLoadingProgress!=null){ mLoadingProgress.dismiss(); } } }
//自定義的佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/x240"
    android:layout_height="@dimen/x240"
    android:background="@drawable/loading_bg"
    android:orientation="vertical"
    android:gravity="center"
    >
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/loading_pb"
        <!--用於改變圓圈的顏色-->
        android:indeterminateTint="@color/colorAccent"
        android:indeterminateTintMode="src_atop"

    />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="正在載入..."
        android:textSize="16sp"
        android:id="@+id/loading_tv"
        />


    </LinearLayout>

如何使用,很簡單在Activity,或Fragment中:比如下面觸發按鈕響應

 bt_wait.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            //只要一行程式碼
                LoadingCustom.showprogress(getContext(),"正在載入",true);
//如果伺服器響應資料返回我們只要呼叫  LoadingCustom.dismissProgress();即可
            }
        });
/**<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="8dp"></corners>
    <solid android:color="@android:color/darker_gray"></solid>

</shape>
*/