Android - displaying text in center of progress ba

2019-02-08 01:49发布

问题:

I have already made a horizontal progress bar and it works perfectly. I would like to display a textview or something similar right in the middle of it showing a countdown as the bar is loading. Keep in mind its not a progress dialog, progress bar resides inside an activity and shows a countdown timer. Can anyone help me out, whats the best way to do this and how can I accomplish this?

回答1:

If your ProgressBar and TextView are inside a RelativeLayout you can give the ProgressBar an id, and then align the TextView with the ProgressBar using that. It should then show on top of the ProgressBar. Make sure the background is transparent so that you can still see the ProgressBar

For example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        // other attributes
        android:id="@+id/PROGRESS_BAR"
    >
    <TextView
        android:background="#00000000" // transparent
        // other attributes
        android:layout_alignLeft="@id/PROGRESS_BAR"
        android:layout_alignTop="@id/PROGRESS_BAR"
        android:layout_alignRight="@id/PROGRESS_BAR"
        android:layout_alignBottom="@id/PROGRESS_BAR"
    >
</RelativeLayout>


回答2:

You can use a FrameLayout to display the TextView over the ProgressBar:

...
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/progress"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:progressDrawable="@drawable/progressbar" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true" />
        ...
    </RelativeLayout>
</FrameLayout>


回答3:

that is what worked for me:

             <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                  <ProgressBar
                      android:id="@+id/progressBar"
                      style="@android:style/Widget.ProgressBar.Horizontal"
                      android:progressDrawable="@drawable/customprogressbar"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:max="100"/>

                  <TextView
                      android:id="@+id/progressBarinsideText"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentLeft="true"
                      android:layout_alignParentRight="true"
                      android:layout_centerVertical="true"
                      android:gravity="center"
                    />
              </RelativeLayout>


回答4:

This is what I used to display progress text above a spinner centered on a web view:

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

    <WebView
        android:id="@+id/help_webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#F22"
        android:scrollbars="none" />


    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/progressBarMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading, please wait..."
        android:layout_centerInParent="true"
        android:layout_above="@id/progressBar"
        android:background="#00000000" />


</RelativeLayout>


回答5:

Source tutorial

In Activity we can Show and Dismiss ProgressBar using the following methods

// Method to show Progress bar
private void showProgressDialogWithTitle(String substring) {
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    //Without this user can hide loader by tapping outside screen
    progressDialog.setCancelable(false);
    progressDialog.setMessage(substring);
    progressDialog.show();
}

// Method to hide/ dismiss Progress bar
private void hideProgressDialogWithTitle() {
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.dismiss();
}

Check other