Android:如何在中间显示全屏进度条

时间:2022-06-01 23:44:42

I am trying to display progress bar with custom theme. But it is coming in top left corner instead of center. If I don't use custom theme it will come in center but not full screen, which displays textviews and other elements in background which looks bad. The following is the code and screenshot

我正在尝试显示带有自定义主题的进度条。但是它在左上角而不是中间。如果我不使用自定义主题,它会出现在中间,而不是全屏,显示文本视图和其他元素在背景中,看起来很糟糕。下面是代码和屏幕截图

Custom Theme:

自定义主题:

<?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">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

In activity initialising progress dialog with custom theme:

在活动初始化进程对话框自定义主题:

progressBar = new ProgressDialog(context, R.layout.layout_progress_dialog);
progressBar.setMessage("Please wait...");
progressBar.setCancelable(false);

Android:如何在中间显示全屏进度条

When I see the preview of progress bar theme alone it shows correctly as below. But in activity it shows in wrong position.

当我单独看到进度条主题的预览时,它会正确地显示如下所示。但在活动中,它显示出了错误的位置。

Android:如何在中间显示全屏进度条

3 个解决方案

#1


1  

You can use the <include/> tag to add the progress bar to your activity's layout and set it's location. If your custom style is customProgressDialog.xml :

您可以使用 标记将进度条添加到活动的布局中,并设置它的位置。如果您的自定义样式是customProgressDialog。xml:

<include
    layout="@layout/customProgressDialog"
    android:id="@+id/customProgressDialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
/>

Then in your activity :

然后在你的活动中:

ProgressDialog dialog = (ProgressDialog) findViewById(R.id.customProgressDialog);

#2


0  

You are passing the id of a layout in a constructor that requires the id of a theme.

在需要主题id的构造函数中传递布局的id。

progressBar = new ProgressDialog(context, R.layout.layout_progress_dialog);

The constructor is

构造函数是

public ProgressDialog(Context context, int theme) 

You should define your custom attributes in style.xml and pass the id of that style here.

您应该用样式定义自定义属性。在这里传递该样式的id。

#3


0  

Try setting android:layout_centerHorizontal="true" and android:layout_centerVertical="true"

尝试设置android:layout_centerHorizontal="true"和android:layout_centerVertical="true"

#1


1  

You can use the <include/> tag to add the progress bar to your activity's layout and set it's location. If your custom style is customProgressDialog.xml :

您可以使用 标记将进度条添加到活动的布局中,并设置它的位置。如果您的自定义样式是customProgressDialog。xml:

<include
    layout="@layout/customProgressDialog"
    android:id="@+id/customProgressDialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
/>

Then in your activity :

然后在你的活动中:

ProgressDialog dialog = (ProgressDialog) findViewById(R.id.customProgressDialog);

#2


0  

You are passing the id of a layout in a constructor that requires the id of a theme.

在需要主题id的构造函数中传递布局的id。

progressBar = new ProgressDialog(context, R.layout.layout_progress_dialog);

The constructor is

构造函数是

public ProgressDialog(Context context, int theme) 

You should define your custom attributes in style.xml and pass the id of that style here.

您应该用样式定义自定义属性。在这里传递该样式的id。

#3


0  

Try setting android:layout_centerHorizontal="true" and android:layout_centerVertical="true"

尝试设置android:layout_centerHorizontal="true"和android:layout_centerVertical="true"