安卓 - 在进度条中央显示文本

44

我已经制作了一个水平进度条,它运行得非常完美。 我想在其中显示一个类似文本视图的东西,并在加载时显示倒计时。 请记住,这不是进度对话框,进度条驻留在活动内并显示倒计时器。 有人可以帮我吗?如何最好地做到这一点,我该如何实现?

8个回答

47
如果您的ProgressBarTextView位于RelativeLayout中,您可以为ProgressBar指定一个id,然后使用该id将TextViewProgressBar对齐。这样TextView就应该显示在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"
        android:id="@+id/PROGRESS_BAR"/>

    <!-- TextView with transparent background -->
    <TextView
        android:background="#00000000"
        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>

只有这个 android:layout_alignTop="@id/simpleProgressBar" 对我有效。 - Diego Venâncio
很棒的解决方案! - Mr. Nicky
3
需要在textView和ProgressBar元素上添加闭合标签。 - Ben Schmidt
如果用户引入字体缩放,就会变得棘手。进度条边界不会围绕着扩展的文本进行换行。 - Otieno Rowland

14

您可以使用FrameLayout在进度条上方显示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>

好的解决方案。不确定相对布局的目的 - 似乎是多余的。 - dazed
@dazed 应该使用FrameLayout来容纳单个子视图,因为在不同的屏幕尺寸上组织子视图并使它们不重叠可能很困难。但是,您可以向FrameLayout添加多个子元素,并通过为每个子元素分配重力,使用android:layout_gravity属性来控制它们在FrameLayout中的位置。总之,RelativeLayout更适合当您想要放置许多视图并相对地组织它们时。 - androidevil

6
您可以将RelativeLayout放入LinearLayout中,并使RelativeLayout的高度宽度等于wrap_content,然后在进度条下方的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>

3

这是对我有效的方法:

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

2
我在使用框架布局,并将文本放置在框架布局中。
 <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>

这是我得到的输出结果。 这里输入图片描述

非常感谢!我一直在尝试实现这个,但是一直没有成功。感谢您还包括了输出图像! - Shubham Nanche
“@style/CustomProgressBarHorizontal” 是什么样子? - francis

1
这是我用来在网页视图中心显示旋转进度条上方文本的方法:
<?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>

1
你可以将进度条和文本视图放在一个相对布局中,并在这两个子元素的 XML 文件中添加以下行:
android:layout_centerInParent="true"

这应该会强制让ProgressBar和TextView在同一个RelativeLayout的中心显示。
如果您想在Circular ProgressBar内显示Textfield,这也可以使用。
PS. 如果您希望在进度条上方或下方(垂直方向)显示文本视图,则可以调整TextView的'margin'元素。

-5

源教程

enter image description here

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

请检查其他


5
您的链接提供了一个关于已过时的ProgressDialog的操作指南。问题是询问ProgressBar,这是完全不同的事情。 - ChumiestBucket
问题与进度条有关,而不是进度对话框。 - Rahul Hawge

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接