我正在尝试找到一种方法,在我的Android项目中需要一个单独的常见ProgressBar
到处(Activity
/ Fragment
)。我试过的方法没有给我很好的结果。请帮我找一个更好的解决方案。一个代码为android java的例子非常受欢迎。
这是我的Custom ProgressBar
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fl_common_probar"
android:elevation="40dp"
android:visibility="gone"
android:layout_centerInParent="true">
<View
android:id="@+id/background_dim_pro"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.5"
android:background="@color/textTitleW"
android:elevation="50dp"/>
<RelativeLayout
android:id="@+id/relative_container_pro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:elevation="60dp">
<ImageView
android:id="@+id/image_test"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="@drawable/logo_loading" />
<ProgressBar
android:id="@+id/progress_bar_circle_pro"
style="?android:attr/progressBarStyle"
android:layout_width="132dp"
android:layout_height="132dp"
android:indeterminateDrawable="@drawable/circular_progress_bar"
android:indeterminateDuration="@android:integer/config_longAnimTime" />
</RelativeLayout>
请尝试以下方法。
1.)使用以下内容创建一个layout xml
文件(dialog_progress.xml
)。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_progress"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
2.)创建AppUtil Java class
并创建如下的静态方法。
public class AppUtil {
// progress bar handling
public static Dialog showProgress(Activity activity) {
Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(0));
dialog.setContentView(R.layout.dialog_progress);
ProgressBar progressBar = dialog.findViewById(R.id.progressBar);
progressBar.getIndeterminateDrawable().setColorFilter(activity.getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.MULTIPLY);
dialog.setCancelable(false);
dialog.show();
return dialog;
}
}
3.)当你需要在qazxsw poi / qazxsw poi中显示qazxsw poi时,如下所示。
ProgressBar
当你需要Activity
Fragment
Dialog dialog = AppUtil.showProgress(MainActivity.this);
而已。试着告诉我你的反馈意见。谢谢。
dissmiss
活动:
progressBar