Java 如何使ProgressDialog背景覆盖整个屏幕?
我正在使用以下代码添加进度对话框: progress\u dialog.xmlJava 如何使ProgressDialog背景覆盖整个屏幕?,java,android,android-studio,Java,Android,Android Studio,我正在使用以下代码添加进度对话框: progress\u dialog.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" and
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:windowIsFloating="false"
android:background="@android:color/white">
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_centerInParent="true" />
</RelativeLayout>
这将显示进度对话框,如下所示:
我希望进度对话框覆盖整个屏幕,如下所示:
如何使用
ProgressDialog
?设置宽度和高度以匹配progressbar中的父项
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_centerInParent="true" />
试试这个:
首先,您必须在样式文件中创建主题,如下所示:
<style name="CustomDialogTheme" parent="Theme.AppCompat.Light.DialogWhenLarge">
<item name="android:windowIsFloating">false</item>
<item name = "android:windowBackground" >@android:color/white</item >
</style>
最后,这是自定义布局:
<?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">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
这很好,但我建议您在将来需要时将其用于不同的类,只要调用类名并实现即可
public class ShowDialogClass {
public static ProgressDialog showDialog(Context context) {
final Dialog dialog = new Dialog(context, R.style.CustomDialogTheme);//apply style here
View view = LayoutInflater.from(context).inflate(R.layout.progress, null);// inflate the custom view
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT; // set as full width
params.height = WindowManager.LayoutParams.MATCH_PARENT;// set as full height
dialog.setContentView(view); //add the custom view
dialog.getWindow().setGravity(Gravity.CENTER); // set center gravity
return dialog;
}
}
您可以如何使用它,例如:
Dialog dialog= ShowDialogClass.showDialog(this)
dialog.show()
dialog.dismiss()
public class ShowDialogClass {
public static ProgressDialog showDialog(Context context) {
final Dialog dialog = new Dialog(context, R.style.CustomDialogTheme);//apply style here
View view = LayoutInflater.from(context).inflate(R.layout.progress, null);// inflate the custom view
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT; // set as full width
params.height = WindowManager.LayoutParams.MATCH_PARENT;// set as full height
dialog.setContentView(view); //add the custom view
dialog.getWindow().setGravity(Gravity.CENTER); // set center gravity
return dialog;
}
}
Dialog dialog= ShowDialogClass.showDialog(this)
dialog.show()
dialog.dismiss()