简体   繁体   中英

Android: How to display full screen progress bar in center

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);

在此输入图像描述

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

在此输入图像描述

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

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.

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 :

<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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM