我目前正在尝试找出一种在我的Android项目中需要到每个地方(ProgressBar
/ Activity
)的单个Common Fragment
的方法。我尝试的方法并不能保证我会取得好的结果。请帮助我找到更好的解决方案。一个带有android java代码的示例非常受欢迎。
这是我的自定义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.)当需要在
ProgressBar
/ Activity
中显示Fragment
时,请执行以下操作。Dialog dialog = AppUtil.showProgress(MainActivity.this);
当您需要
dissmiss
时progressBar
dialog.dismiss();
而已。尝试让我知道您的反馈。
谢谢。