我有以下加載程序 xml -custom_progress_dialog.xml(布局)<?xml version="1.0" encoding="utf-8" ?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/progres" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:background="#000000" > <ProgressBar android:indeterminate="true" style="?android:attr/progressBarStyleLarge" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="20dp" android:layout_centerInParent="true" android:indeterminateDrawable="@drawable/custom_progress_bar" /></RelativeLayout>custom_progress_bar.xml(可繪制)<?xml version="1.0" encoding="utf-8" ?><rotate xmlns:android="http://schemas.android.com/apk/res/android" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0" android:toDegrees="360"> <shape android:shape="ring" android:innerRadiusRatio="3" android:thicknessRatio="8" android:useLevel="false"> <size android:width="76dip" android:height="76dip" /> <gradient android:type="sweep" android:useLevel="false" android:startColor="@android:color/transparent" android:endColor="#00FF00" android:angle="0" /> </shape></rotate>然后有一個ShowLoading方法 -public static Dialog ShowLoading(Context context){ Dialog dialog = new Dialog(context, Android.Resource.Style.ThemeTranslucentNoTitleBar); dialog.SetContentView(Resource.Layout.custom_progress_dialog); dialog.Window.SetGravity(GravityFlags.Center); dialog.SetCancelable(true); dialog.CancelEvent += delegate { dialog.Dismiss(); }; dialog.Show(); return dialog;}我遇到的問題是,當我調用 Web 服務時,它沒有顯示進度條,并且應用程序基本上凍結,直到對 Web API 的調用完成。我什至從方法中刪除了 web api 調用,以測試進度條在調用時是否正確顯示。如何在 api 調用進行時顯示進度條?
1 回答

梵蒂岡之花
TA貢獻1900條經驗 獲得超5個贊
不要在主線程調用api,使用AsyncTask調用api
像
public class MyAsync extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
//show progress dialog here
}
@Override
protected Void doInBackground(Void... voids) {
//call api here
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//hide progress dialog here
}
}
執行它使用這個
new MyAsync().execute();
- 1 回答
- 0 關注
- 72 瀏覽
添加回答
舉報
0/150
提交
取消