Android - 在进度条的中心显示文本

时间:2013-08-23 20:06:07

标签: android progress-bar

我已经制作了一个水平进度条,它完美无缺。我想在栏中间显示一个textview或类似的东西,显示倒计时栏正在加载。请记住,它不是进度对话框,进度条位于活动内部并显示倒数计时器。任何人都可以帮助我,最好的方法是什么,我怎么能做到这一点?

8 个答案:

答案 0 :(得分:38)

如果您的ProgressBarTextView位于RelativeLayout内,则可以为ProgressBar提供ID,然后将TextView与{{1}对齐使用它。然后它应显示在ProgressBar之上。确保背景是透明的,这样您仍然可以看到ProgressBar

例如:

ProgressBar

答案 1 :(得分:10)

您可以使用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>

答案 2 :(得分: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>

答案 3 :(得分: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>

答案 4 :(得分:1)

您可以在内部设置 RelativeLayout LinearLayout ,并使RelativeLayout Height width 等于< strong> 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>

答案 5 :(得分:0)

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

android:layout_centerInParent="true"

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

如果要在圆形进度栏中显示文本字段,这也可以使用。

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

答案 6 :(得分:0)

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

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

这是我得到的输出

enter image description here

答案 7 :(得分:-1)

Source tutorial

在“活动”中,我们可以使用以下方法显示和关闭 ProgressBar

template<class DerivedT>
struct CuriousBase{};

template<template<typename> typename... Features>
struct X : Features<X<Features...>> ... {
  X() = default;
  X(Features<X> ...f) : Features<X>(f)... {}
};

int main(){
  auto x = X<CuriousBase>{};
}

检查其他

相关问题