40

我已经制作了一个水平进度条,并且效果很好。我想在它的中间显示一个文本视图或类似的东西,在加载条时显示倒计时。请记住,它不是进度对话框,进度条位于活动内并显示倒数计时器。任何人都可以帮助我,最好的方法是什么,我怎样才能做到这一点?

4

8 回答 8

44

如果您的ProgressBarandTextView在 a 内,RelativeLayout您可以提供ProgressBar一个 id,然后将其TextViewProgressBarusing 对齐。然后它应该显示在ProgressBar. 确保背景是透明的,以便您仍然可以看到ProgressBar

例如:

<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>
于 2013-08-23T20:52:37.670 回答
13

您可以使用 FrameLayout 在 ProgressBar 上显示 TextView:

...
<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>
于 2014-02-12T16:40:12.120 回答
5

您可以在其中设置 带有RelativeLayout的LinearLayout并使 RelativeLayout高度宽度等于wrap_content然后在 ProgressBar 下的 TextView 中添加 android: layout_centerInParent="true"

例子 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:max="100"
            android:progress="65" />
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_centerInParent="true"
            android:textColor="@color/white"
            android:text="6:00 AM"
            android:textSize="25sp"
            android:textStyle="bold"/>
    </RelativeLayout>
</LinearLayout>
于 2019-08-22T03:29:15.043 回答
4

这对我有用:

             <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>
于 2016-11-23T14:58:44.077 回答
1

在这里,我使用框架布局并将文本放入框架布局中

 <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="30dp"
        map:layout_constraintBottom_toBottomOf="@+id/programStatusTextView"
        map:layout_constraintStart_toEndOf="@+id/programStatusTextView"
        map:layout_constraintTop_toTopOf="@+id/programStatusTextView">

        <RelativeLayout
            android:layout_width="130dp"
            android:layout_height="60dp"
            android:background="@drawable/battery">

            <ProgressBar
                android:id="@+id/batteryIndicaterBar"
                style="@style/CustomProgressBarHorizontal"
                android:layout_width="119dp"
                android:layout_height="52dp"
                android:layout_marginStart="3dp"
                android:layout_marginTop="4dp"
                android:layout_marginEnd="7dp"
                android:backgroundTint="@color/colorPrimary"
                android:max="100"
                android:minHeight="10dip"
                android:paddingEnd="4dp"
                android:progress="0"
                android:progressBackgroundTintMode="src_over"
                android:progressTint="@color/green"
                android:visibility="visible" />

        </RelativeLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="0"
            android:textSize="20sp"
            android:textStyle="bold" />

    </FrameLayout>

这是我得到的输出

在此处输入图像描述

于 2020-09-03T12:50:42.593 回答
1

这是我用来在以 Web 视图为中心的微调器上方显示进度文本的内容:

<?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>
于 2018-02-14T19:34:55.810 回答
0

您可以将 ProgressBar 和 TextView 放在相对布局中,并将以下行添加到这两个子 XML 文件中:

android:layout_centerInParent="true"

这应该强制 ProgressBar 和 TextView 都显示在同一个相对布局的中心。

如果您想在 Circular ProgressBar 中显示 Textfield,这也可以工作。

PS。如果您还想在进度条上方或下方(垂直)显示 textview,则可以调整 TextView 的“margin”元素。

于 2020-07-17T00:14:26.340 回答
-3

源码教程

在此处输入图像描述

在 Activity 中,我们可以使用以下方法显示和关闭ProgressBar

// 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();
}

检查其他

于 2018-08-16T07:11:33.140 回答